How to find the largest prime factor of 600851475143 in C? [duplicate]

≯℡__Kan透↙ 提交于 2019-12-13 23:26:48

问题


I try to find the largest prime factor of 600851475143 with the code as given below.
However, this code does not return any output, not even errors or warnings.

Where do I go wrong?

Code:

    #include<stdio.h>

int main()
{
  int i,j,k,mpf;
  for (i=1;i<600851475143;i++)
  {
    if(600851475143%i==0)
    {
      k=0;
      for(j=1;j<=i;j++)
      {
        if(i%j==0)
        {
          k++;
        }
      }
      if(k==2)
      {
        mpf=i;
      }
    }
  }
  printf("\nThe largest prime factor of 600851475143 is: %ld\n",mpf);
  return 0;
}

回答1:


Looping up to 600 billion will take forever — not just a long time, but impossible because it exceeds what can be stored in an int (usually up to 2^31-1 these days, but could be as small as 2^15-1 according to the C standard).

What you need is a smarter algorithm. Rather than giving you the solution verbatim, I suggest that you look at existing answers and work out a solution in C by yourself.




回答2:


Interesting. As INT_MAX is likely less than 600851475143, incrementing i will never meet/exceed 600851475143 --> infinite loop. Be sure to use integer types that support the needed range.

// infinite loop with 32-bit `int`
int i;
for (i = 1; i < 600851475143; i++) {

Recommend changes commented in code.

#include<math.h>
#include<stdio.h>

int main(void) {
  //int i, j, k, mpf;
  unsigned long i, j, k, mpf = 0;
  // 600851475143 is likely outside range of `int`,
  //   better to treat it as an `unsigned long long`
  // no need to try values > sqrt(600851475143)
  //for (i = 1; i < 600851475143u; i++) {
  unsigned long maxi = (unsigned long) ceil(sqrtl(600851475143u));
  for (i = 1; i <= maxi; i++) {
    if (600851475143u % i == 0) {
      k = 0;
      for (j = 1; j <= i; j++) {
        if (i % j == 0) {
          k++;
        }
      }
      if (k == 2) {
        mpf = i;

      }
    }
  }
  // Use matching specifiers: Notice the mis-match          long    int
  // printf("\nThe largest prime factor of 600851475143 is: %ld\n", mpf);
  printf("\nThe largest prime factor of 600851475143 is: %lu\n", mpf);
  return 0;
}

Other improvements for OP: start from largest candidate divisor and work down to 1. Upon finding the first factor, quit loop. Drop the k code.




回答3:


Instead of looping from 1 to 600851475143, you should loop from 2 to the square root of 600851475143:

long long num=600851475143;
long i=2;   
while(i<=sqrt(num))
{   
    //printf("%lu\n",num);
    if(num%i==0)
    {
        while(num%i==0)
        {
            num/=i;
        }           
        if(num==1)
        {
            num=i;
            break;
        }
    }
        else
        {
            i++;
        }
}
printf("%lu",num);


来源:https://stackoverflow.com/questions/30515940/how-to-find-the-largest-prime-factor-of-600851475143-in-c

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!