原题
Largest prime factor
Problem 3
The prime factors of 13195 are 5, 7, 13 and 29.
What is the largest prime factor of the number 600851475143 ?
分析
直接用筛素数法找number的因数,最大的因数就是要求的结果
代码
#coding=utf-8
def getList(n):
result = [] #定义空列表
count = 3 #从3开始遍历,因为2单独判断
max = n #max为初始数值
while max%2 == 0: #以2作判断,如果能被2整除,则2加入列表,max除以2——把里面的2全部除完
max /= 2
result.append(2) # 表示有2这个素因数
while count <= max: #以max大于count开始循环
# 类似筛素数的方法,count一定是素数
if max%count == 0: #如果max能被count整除,把count加入列表,max除以count,跟上面对2的判断类似 —— 并且一旦能整出,则把count除尽
result.append(count)
max /= count
else:
count += 2 #因为2已经除完,所以排除偶数
return result
print getList(102) #得到所有素因数