[英]Some numbers take longer to factor than others
Can someone tell me the reason that the number 104751475143 is computed in less than a second while the number 251475141 takes a longer amount of time? 有人可以告诉我为什么数字104751475143在不到一秒钟的时间内计算出来,而数字251475141却花费了更长的时间吗? The code is trying to find the largest factor. 该代码试图找到最大的因素。
This is my code. 这是我的代码。
long long lrgPFactor = 0;
long long currentfactor = 0;
long long tempfactor = 0;
long long Number = 251475141;
long long factor = 0;
switch ((long long)sqrt(Number)%2) //skipping to the square root of the number to save time
{
case 0: factor=((long long)sqrt(Number)-1); break; //If even make it odd
default: factor=((long long)sqrt(Number)); break; //If odd leave it
}
while (factor > 0)
{
if (Number % factor == 0) //if factor is a factor
{
factor=Number/factor; //make the factor the larger of the pair
switch ((long long)sqrt(factor)%2) //same as above
{
case 0: tempfactor=((long long)sqrt(factor)-1); break;
default: tempfactor=((long long)sqrt(factor)); break;
}
for (tempfactor = factor - 1; tempfactor > 1; --tempfactor) //simple way to determine if prime
{
if (factor % tempfactor == 0)
break;
}
if (tempfactor == 1)
{
lrgPFactor = factor;
break;
}
}
factor -= 2;
}
因为104751475143的质因子为391751和104751475143/391751 = 267393(这更接近104751475143的平方根)比3(3 = 251475141/83825047)更接近251475151的平方根,所以花费的时间更长。循环的迭代。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.