Created
July 11, 2013 13:26
-
-
Save Ayrx/5975421 to your computer and use it in GitHub Desktop.
Prime factorization snippet
This file contains 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 prime_factors(n): | |
"""Returns all the prime factors of a positive integer""" | |
factors = [] | |
d = 2 | |
while n > 1: | |
while n % d == 0: | |
factors.append(d) | |
n /= d | |
d += 1 | |
if d*d > n: | |
if n > 1: | |
factors.append(n) | |
break | |
return factors |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment