Created
August 8, 2015 21:29
-
-
Save cengiz-io/2f3bfe774ebc524a44f1 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #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