Skip to content

Instantly share code, notes, and snippets.

@mdjhny
Last active December 14, 2015 06:48
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 mdjhny/5044904 to your computer and use it in GitHub Desktop.
Save mdjhny/5044904 to your computer and use it in GitHub Desktop.
Python分解因数
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