mercredi 14 avril 2021

Trying out a problem but code I had written is not giving output for large numbers. why?

I was trying this question.

The prime factors of 13195 are 5, 7, 13 and 29.What is the largest prime factor of the number 600851475143 ?

And I had written the following code:

#include<iostream>
#define num 600851475143
using namespace std;
int isprime(unsigned long long int n)
{
  unsigned long long int c=0;
  for(unsigned long long int i=2;i<n;i++)
  {
      if(n%i==0)
      {
          c++;
          break;
     }
  }
  if(c==0)
  {
      return 1;
  } 
  else
  {
      return 0;
  }
 }
 int main()
 {
 unsigned long long int a,i,n=num;
 while(n-- && n>1)
 {  
     if(isprime(n)==1 && num%n==0)
     {
         cout<<n;
         break;
     }
 }
 return 0;
}

The problem occurring with the code is it is working for 13195 and other small values. But not getting any output for 600851475143. Can anyone explain why it is not working for large value and also tell the changes that should be made in these to get the correct output.

Aucun commentaire:

Enregistrer un commentaire