So here are two functions for finding the prime factors of a number. Credits: Triptych https://stackoverflow.com/a/412942/6211963
def prime_factors1(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 = d + 1
return factors
def prime_factors2(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 = d + 1
if d*d > n:
if n > 1: factors.append(n)
break
return factors
Obviously the second code runs a lot faster, but why does it output the largest factor as long-type rather than int?
>>> prime_factors1(65126264424)
[2, 2, 2, 3, 13, 29, 7197863]
>>> prime_factors2(65126264424)
[2, 2, 2, 3, 13, 29, 7197863L]