Skip to content

Instantly share code, notes, and snippets.

@biesnecker
Created September 30, 2013 21:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save biesnecker/6770777 to your computer and use it in GitHub Desktop.
Save biesnecker/6770777 to your computer and use it in GitHub Desktop.
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
# 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