from itertools import count, islice, takewhile
def prime_sieve2():
sieved = count(2)
prime = next(sieved)
yield prime
primes = [prime]
for x in sieved:
possible_prime_divs = takewhile(lambda p: p <= x**0.5, primes)
if any(x % prime == 0 for prime in possible_prime_divs):
continue
yield x
primes.append(x)
if __name__ == '__main__':
print("Show the 10,001st prime.\n =",
next(islice(prime_sieve2(), 10_000, 10_001)))
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
Modified to test less primes: