Created
September 30, 2013 21:46
-
-
Save biesnecker/6770777 to your computer and use it in GitHub Desktop.
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
from math import sqrt, ceil, trunc | |
# Sieve of Eratosthenes | |
def seive(max): | |
nums = range(max + 1) | |
nums[0] = None | |
nums[1] = None | |
for i in range(2, max + 1): | |
if nums[i]: | |
c = i * 2 | |
while c <= max: | |
nums[c] = None | |
c += i | |
yield i | |
val = 600851475143 | |
#val = 13195 | |
max_factor = trunc(ceil(sqrt(val))) | |
primes = [i for i in seive(max_factor)] | |
for i in range(len(primes) - 1, -1, -1): | |
if val % primes[i] == 0: | |
print primes[i] | |
break |
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
# prime generator based on a modification of the Seive of Eratosthenes | |
# that allows it to generate an infinite series of primes | |
def primes(): | |
d = {} | |
q = 2 | |
while True: | |
if q not in D: | |
yield q | |
d[q * q] = [q] | |
else: | |
for p in D[q]: | |
d.setdefault(p + q, []).append(p) | |
del d[q] | |
q += 1 | |
p = primes() | |
for i in range(10000): | |
p.next() | |
print "%d" % p.next() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment