Created
July 30, 2020 14:29
-
-
Save DhruvaG2000/027b1665f36ca79bff86de23762bb154 to your computer and use it in GitHub Desktop.
This works
This file contains 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> | |
long long max_prime_factor(long long num); | |
int main(void) { | |
std::cout << max_prime_factor(600851475143LL) << std::endl; | |
return 0; | |
} | |
long long max_prime_factor(long long num) { | |
// 2 is the only even prime number | |
while (num % 2 == 0) { | |
num /= 2; | |
} | |
long long max = 2; | |
long long factor = 3; | |
while (num > 1) { | |
while (num % factor == 0) { | |
num /= factor; | |
max = factor; | |
} | |
factor += 2; | |
} | |
return max; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment