Mandrew

ProjectEuler problem 3 (unverified)

Jul 24th, 2012
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.82 KB | None | 0 0
  1. /* ProjectEuler.net Problem 3
  2.  
  3. The prime factors of 13195 are 5, 7, 13 and 29.
  4.  
  5. What is the largest prime factor of the number 600851475143 ? */
  6. #include <stdio.h>
  7. #include <cstdlib>
  8. int findThatLargestPrime(const long &hugeNumber)
  9. {
  10.     static long thatPrime = hugeNumber - 1;
  11.     do { thatPrime--; } while (hugeNumber%thatPrime);
  12.     bool didItFail=false;
  13.     for ( long i=thatPrime-1; i == 2; i-- )
  14.     {
  15.         long primeTest = thatPrime%i;
  16.         if (!primeTest)
  17.         {
  18.             didItFail=true;
  19.             break;
  20.         }
  21.     }
  22.     if (!didItFail) { printf("%lu is a factor but is not prime\n",thatPrime); return -1; } else { return thatPrime; }
  23. }
  24.  
  25. int main(int argc, char *argv[])
  26. {
  27.     const ulong bigNumber = 600851475143;
  28.     long hitGold;
  29.     do {
  30.         hitGold = findThatLargestPrime(bigNumber);
  31.     } while (hitGold==-1);
  32.     printf("%ld\n",hitGold);
  33.     return 0;
  34. }
Advertisement
Add Comment
Please, Sign In to add comment