Created
October 17, 2013 20:10
-
-
Save nsfyn55/7031407 to your computer and use it in GitHub Desktop.
Sieve of Erasthostenes
This file contains hidden or 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
def create_list(n): | |
l = [] | |
for x in range(2,n+1): | |
l.append(x) | |
return l | |
def get_remove_factors(start, n, inc): | |
s = set() | |
for i in range(start, n+1, inc): | |
if i > start: | |
s.add(i) | |
return s | |
def get_next_prime(l, last_prime, non_primes): | |
if last_prime < 2: | |
return 2 | |
for i in [x for x in l if x > last_prime]: | |
if i not in non_primes: | |
return i | |
return None | |
n = 25 | |
next_prime = 2 | |
non_primes = set() | |
while next_prime: | |
non_primes = non_primes.union(get_remove_factors(next_prime, n, next_prime)) | |
next_prime = get_next_prime([x for x in range(2,n+1)], next_prime, non_primes) | |
print(non_primes) | |
print(set([x for x in range(2, n+1)]).difference(non_primes)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment