Skip to content

Instantly share code, notes, and snippets.

@DhruvaG2000
Created July 30, 2020 14:29
Show Gist options
  • Save DhruvaG2000/027b1665f36ca79bff86de23762bb154 to your computer and use it in GitHub Desktop.
Save DhruvaG2000/027b1665f36ca79bff86de23762bb154 to your computer and use it in GitHub Desktop.
This works
#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