Created
March 14, 2011 11:59
-
-
Save j0hn/869040 to your computer and use it in GitHub Desktop.
Prime criba
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
#!/usr/bin/env python | |
import sys | |
import time | |
def f(k): | |
return lambda j: (j <= k) or (j%k != 0) | |
def criba(i, version=1): | |
p = range(2, i) | |
k = 0 | |
veces = 0 | |
comienzo = time.time() | |
if version == 1: | |
for k in p: | |
veces += 1 | |
p = p[:k] + filter(f(k), p[k:]) | |
else: | |
while k < len(p): | |
p = p[:k] + filter(f(p[k]), p[k:]) | |
k += 1 | |
veces = k | |
tiempo = time.time() - comienzo | |
print "Se corrio %d veces y tardo %.3f segundo" % (veces, tiempo) | |
return p | |
if __name__ == "__main__": | |
if len(sys.argv) > 1: | |
try: | |
num = int(sys.argv[1]) | |
except ValueError: | |
print "Argument must be number" | |
sys.exit(1) | |
else: | |
num = 1000 | |
c1 = criba(num) | |
c2 = criba(num, 2) | |
#print "Resultado:", c2 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment