Created
August 12, 2017 08:53
-
-
Save jniemann66/591699567992acbb975317fa1a9174fb to your computer and use it in GitHub Desktop.
The way I factorize an integer into prime factors ...
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
// factorize() - factorize an integer into prime factors | |
std::vector<int> factorize(int n) { | |
std::vector<int> factors; | |
int maxFactor = std::sqrt(n); | |
for (int factor = 2; factor <= maxFactor; factor++) { | |
while (n % factor == 0) { | |
factors.push_back(factor); | |
n /= factor; | |
} | |
if (n == 1) | |
return factors; | |
} | |
factors.push_back(n); | |
return factors; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment