Last active
December 14, 2015 06:48
-
-
Save mdjhny/5044904 to your computer and use it in GitHub Desktop.
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
def factorize(n): | |
'''Adapted from http://www.math.utah.edu/~carlson/notes/python.pdf''' | |
if n < 2: | |
return '本函数仅适用于不小于2的正整数' | |
d = 2 | |
factors = [] | |
while not n % d: | |
factors.append(d) | |
n /= d | |
d = 3 | |
while n > 1 and d * d <= n: | |
if not n % d: | |
factors.append(d) | |
n /= d | |
else: | |
d += 2 | |
if n > 1: | |
factors.append(n) | |
return factors |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment