Created
February 2, 2012 09:25
-
-
Save sammyrulez/1722541 to your computer and use it in GitHub Desktop.
Sieve of Eratosthenes
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 compute(n=100): | |
seq = range(2,n) | |
p = 2 | |
while p < n and len(range(2*p,n,p)): | |
jumpy = range(2*p,n,p) | |
for j in jumpy: | |
if seq.count(j) > 0: | |
seq.remove(j) | |
p = seq[1+seq.index(p)] | |
print "Prime nums from 2 to " , n , " : ", seq | |
return seq | |
def test(): | |
expected = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97] | |
out_list = compute() | |
for o in out_list: | |
assert( expected.count(o) > 0 ) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment