我的程序来计算最大的主要因子600851475143,被困住了,在编译和执行期间从未停止。有谁知道为什么它不能完成执行?

#include <stdio.h> //Edited the #includes(typo) to #include
 int main (void)
{

    long long int num = 600851475143 ;
    long long int factorCount;
    long long int bigFactor;

    for ( long long int i=1 ; i <= num; i+=2 )// Iterating through all numbers from 2, smaller than or equal to num
    {
        if ( num % i == 0) // If the number "i" is a factor of num i.e. If "i" perfectly divides num
        {
            factorCount = 0;
            //checking whether a factor "i" , is a prime factor of num
            for ( long long int j=2; j <= i ; j++  ) // Iterating through all numbers from 2, smaller than or equal to "i"
            {
                if ( i % j == 0) // If the number "j" prefectly divides or is a factor of "i"
                {
                    factorCount++; //Add 1 to factorCount
                };
            };

            if ( factorCount == 1 ) // If factorCount has not exceeded 1 i.e., the number "i" is a prime number
            {
                bigFactor = i;
            };
        };

    };
    printf("The largets prime factor of %lli is %lli\n",num,bigFactor );

    return 0;
}

最佳答案

我不确定我是否理解您的问题..所以您只想获得一定数量的最大素数?如果是这种情况,则只需执行以下操作:

#include <stdio.h>

#define NUMBER 600851475143

int main (void)
{
    long long int num = NUMBER;
    long long int factor;

    for (factor=2 ; num != 1; factor++) {
        if (num % factor == 0) {
            num = num / factor;
            factor = factor-1;
        }
    }
    printf("The largets prime factor of %lli is %lli\n",NUMBER, factor);
    return 0;
}


为何有效:找到的第一个素数是数字中最小的素数;最后一个主要因素是最大的因素。因此,一旦找到素数p,就不会存在小于p的素数,因为否则您会发现之前的素数较小。因此,您的下一个质数因子大于p。

10-08 04:37