有些数字比其他数字要长

时间:2013-05-20 05:01:45

标签: c primes

有人可以告诉我数字104751475143在不到一秒钟内计算的原因,而数字251475141需要更长的时间吗?代码试图找到最大的因素。

这是我的代码。

    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;
}

1 个答案:

答案 0 :(得分:1)

需要更长时间,因为104751475143的素因子是391751和104751475143/391751 = 267393,它接近104751475143的平方根而不是3(3 = 251475141/83825047)接近251475151的平方根。所以它必须经历循环的更多迭代。