Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* ProjectEuler.net Problem 3
- The prime factors of 13195 are 5, 7, 13 and 29.
- What is the largest prime factor of the number 600851475143 ? */
- #include <stdio.h>
- #include <cstdlib>
- int findThatLargestPrime(const long &hugeNumber)
- {
- static long thatPrime = hugeNumber - 1;
- do { thatPrime--; } while (hugeNumber%thatPrime);
- bool didItFail=false;
- for ( long i=thatPrime-1; i == 2; i-- )
- {
- long primeTest = thatPrime%i;
- if (!primeTest)
- {
- didItFail=true;
- break;
- }
- }
- if (!didItFail) { printf("%lu is a factor but is not prime\n",thatPrime); return -1; } else { return thatPrime; }
- }
- int main(int argc, char *argv[])
- {
- const ulong bigNumber = 600851475143;
- long hitGold;
- do {
- hitGold = findThatLargestPrime(bigNumber);
- } while (hitGold==-1);
- printf("%ld\n",hitGold);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment