Skip to content

Instantly share code, notes, and snippets.

@cengiz-io
Created August 8, 2015 21:29
Show Gist options
  • Select an option

  • Save cengiz-io/2f3bfe774ebc524a44f1 to your computer and use it in GitHub Desktop.

Select an option

Save cengiz-io/2f3bfe774ebc524a44f1 to your computer and use it in GitHub Desktop.
#include <iostream>
#include <math.h>
using namespace std;
long findLargestPrimeFactor(long n) {
long squareRootOfN = (long) ceil(sqrt(n));
long largestPrimeFactor = -1;
for (long i = 2; i <= squareRootOfN; i++) {
if (n % i == 0) {
long test = findLargestPrimeFactor(n / i);
if (test > largestPrimeFactor) {
largestPrimeFactor = test;
}
}
}
return (largestPrimeFactor != -1) ? largestPrimeFactor : n;
}
int main() {
cout << findLargestPrimeFactor(600851475143) << endl;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment