Skip to content

Instantly share code, notes, and snippets.

@ricrogz
Last active March 25, 2017 11:51
Show Gist options
  • Select an option

  • Save ricrogz/fd2a73d40d45a326b98fca6f93a5b659 to your computer and use it in GitHub Desktop.

Select an option

Save ricrogz/fd2a73d40d45a326b98fca6f93a5b659 to your computer and use it in GitHub Desktop.
Efficient factorization in python
# Seen here: http://stackoverflow.com/questions/16996217/prime-factorization-list
def factorize(n):
primfac = []
d = 2
while d*d <= n:
while (n % d) == 0:
primfac.append(d) # supposing you want multiple factors repeated
n //= d
d += 1
if n > 1:
primfac.append(n)
return primfac
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment