Created
October 16, 2015 23:58
-
-
Save crackwitz/96bb6c194a23274bf283 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
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