Skip to content

Instantly share code, notes, and snippets.

@crackwitz
Created October 16, 2015 23:58
Show Gist options
  • Save crackwitz/96bb6c194a23274bf283 to your computer and use it in GitHub Desktop.
Save crackwitz/96bb6c194a23274bf283 to your computer and use it in GitHub Desktop.
def nextprime(a, primes=[2]):
while True:
if all (a % p > 0 for p in primes):
return a
a += 1
def factorize(n):
factors = []
primes = [2]
p = 2
while True:
if p*p > n:
break
while n % p == 0:
factors.append(p)
n //= p
p = nextprime(p+1, primes)
primes.append(p)
factors.append(n)
return factors
if __name__ == '__main__':
print(factorize(int(sys.argv[1])))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment