【发布时间】:2013-12-28 08:38:42
【问题描述】:
我需要一点帮助:
public class BiggestPrimeFactor{
public static void main(String[] args){
long biggest=0L;
for(long i=2L; i<=600851475143L; i++){
if(600851475143L%i==0){
for(int l=1; l<=Math.sqrt(i); l++){
if (i%l==0){
break;
} else{
biggest=i;
}
}
}
}
System.out.println(biggest);
}
}//end of BiggestPrimeFactor
我不知道是好是坏,但它占用了太多(半个多小时然后我累了并关闭了命令行)......
你能帮忙吗,或者至少告诉我好不好?
谢谢!
我可以解决它!
这就是它的样子
public class BiggestPrimeFactor{
public static void main(String[] args){
long x=600851475143L;
long biggest=0L;
for(long i=2L; i<=x; i++){
for(long l=1L; l<=Math.sqrt(i); l++){
if(l%i==0){
break;
} else{
while(x%i==0){
x=x/i;
biggest =i;
}
}
}
}
System.out.println(biggest);
}
}//BiggestPrimeFactor 结束
而且花费的时间真的很短! =P 感谢您的帮助!
【问题讨论】:
-
那是一个巨大的loooooooooooooooooooop。
-
@MarounMaroun one loooooooooooop 不会成为问题。 Core i7 reaches 100 gigaFLOPS 所以会在 6 秒 内完成。这是第二个 嵌套 循环,它将 6 秒变成 2-4 天 的预计运行时间。 :)(有趣的是,log(600851475143) ~= 27,与一个月内 30 天相比,因此如果嵌套循环没有提前中断,它将运行 2 个月)。
-
您需要
l做什么? -
我用它来检查
i是否是素数 -
你不需要它。此外,为了保存一个
x%i,你需要做很多l%is。
标签: java primes prime-factoring