Objective
Create a Python program with an infinite generator of prime numbers and take only the first ones with itertools.islice.
Instructions
- Define
def primes(), which starts atn = 2and, in awhile True, yieldsnif it is not divisible by any number between 2 and its square root (all(n % d != 0 for d in range(2, int(n ** 0.5) + 1))), and moves on to the next number. - Read an integer
n(at least 1). - Get the first
nprimes withislice(primes(), n)and show them on one line, separated by spaces.
8
2 3 5 7 11 13 17 19
Open the editor in full screen: the statement on the left and your code, the output and the tests on the right.
Start exercise