题目:求600851475143 的最大质因数。

搞了2,3个小时,搞出来的算法效率都很低。网上发现一个nb算法。改成python后如下:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
def maxPrime():
	n=600851475143
	d=2
	while d<n/d:
		if n%d==0:
			n=int(n/d)
		else:
			d=d+1
	return n

if __name__=="__main__":
	from time import clock
	start=clock()
	finish=clock()
	print("the largest prime factor is {0}\t\t{1}s".format(maxPrime(),(finish-start)/1000000))

结果: the largest prime factor is 6857 4.462223927783368e-13s

哎,差距啊~