Created
October 1, 2011 17:34
-
-
Save andlima/1256380 to your computer and use it in GitHub Desktop.
A simple generator of the primes sequence
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
primes_list = [2, 3] | |
def primes(maximum): | |
for p in primes_list: | |
if p > maximum: | |
return | |
yield p | |
for m in range(primes_list[-1] + 2, maximum + 1, 2): | |
sqrt_m = int(m ** 0.5) | |
if all(m % p != 0 for p in primes(sqrt_m)): | |
primes_list.append(m) | |
yield m |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment