Last active
March 25, 2017 11:51
-
-
Save ricrogz/fd2a73d40d45a326b98fca6f93a5b659 to your computer and use it in GitHub Desktop.
Efficient factorization in python
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
| # 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