Skip to content

Instantly share code, notes, and snippets.

@Ayrx
Created July 11, 2013 13:26
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 Ayrx/5975421 to your computer and use it in GitHub Desktop.
Save Ayrx/5975421 to your computer and use it in GitHub Desktop.
Prime factorization snippet
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