Skip to content

Instantly share code, notes, and snippets.

@folz
Created March 3, 2011 07:19
Show Gist options
  • Save folz/852465 to your computer and use it in GitHub Desktop.
Save folz/852465 to your computer and use it in GitHub Desktop.
Project Euler problem #3
'''
The prime factors of 13195 are 5, 7, 13 and 29.
What is the largest prime factor of the number 600851475143 ?
'''
import math
from time import time
calcs = {}
def pfactor(n):
if n < 2:
return [1]
try:
return calcs[n]
except:
x = []
for a in range(2, int(.5 + math.sqrt(n))+1):
if n/a == int(n/a):
x.append(a)
x.extend(pfactor(int(n/a)))
calcs[n] = x
return x
calcs[n] = [n]
return [n]
if __name__ == "__main__":
start = time()
print(pfactor(600851475143)[-1], time() - start)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment