简体   繁体   English

一些数字比其他数字花费的时间更长

[英]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.

相关问题 取一些数字,发现它是否是质数? - Take some numbers & find is it prime or not? 在唤醒线程时,futex如何比互斥体花费更长的时间? - How come a futex take longer than a mutex on waking a thread? 为什么有些数字显示小数而其他数字不显示? - Why do some numbers present the decimals and others don't? 为什么一些算术运算需要比平常更多的时间? - Why some arithmetic operations take more time than usual? 为什么UDP客户端发送数据所需的时间比服务器接收数据所需的时间长? - Why does It take longer for my UDP client to send data than it does for the server to receive it? 为什么一个包含100亿次迭代的大型for循环在Python中运行的时间要比在C中运行的时间长? - Why does a large for loop with 10 billion iterations take a much longer time to run in Python than in C? 为什么py2app .app比同一个python程序需要更长的时间才能启动? - Why does a py2app .app take longer to launch than the same python program? 为什么需要花费这么长时间才能完成100亿到10亿? - Why does it take so much longer to loop through 10 billion than 1 billion? 为什么执行 Java 程序比 C 中的同一程序需要更长的时间? - Why does the execution of a Java program take longer than the same program in C? 我的函数有什么问题,有些数字集起作用而有些数字不起作用? - What is wrong with my function that some sets of numbers worked and others don't?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM