使用 python 的 Euler 项目 #3 - 最有效的方法
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14138053/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Project Euler #3 with python - MOST EFFICIENT METHOD
提问by
I have solved this but I was wondering what the most efficient method of solving this problem was (under 10s). Problem can be found at http://projecteuler.net/problem=3.
我已经解决了这个问题,但我想知道解决这个问题的最有效方法是什么(10 秒以下)。问题可以在http://projecteuler.net/problem=3找到。
采纳答案by bazite
Here is probably the fastest and most compact way of doing it, taking just 141 millisecondsand giving the answer 6857.
这可能是最快和最紧凑的方法,只需141 毫秒并给出答案6857。
The prime factors of 13195 are 5, 7, 13 and 29.
What is the largest prime factor of the number 600851475143 ?
13195 的质因数是 5、7、13 和 29。
数字 600851475143 的最大质因数是多少?
n = 600851475143
i = 2
while i * i < n:
while n % i == 0:
n = n / i
i = i + 1
print n
Code taken from here
代码取自这里

