Skip to content

Instantly share code, notes, and snippets.

@nerdybeast
Last active November 5, 2016 21:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nerdybeast/6ffd1fa62e1253156020da8d4e61a663 to your computer and use it in GitHub Desktop.
Save nerdybeast/6ffd1fa62e1253156020da8d4e61a663 to your computer and use it in GitHub Desktop.
Euler 3 - Largest Prime Factor
'use strict';
function getPrimeFactors(n) {
   
let primeFactors = [];
   
while (n % 2 === 0) {
primeFactors.push(2);
n = n / 2;
}
   
for(var i = 3; i <= Math.sqrt(n); i = i + 2) {
while (n % i === 0) {
primeFactors.push(i);
n = n / i;
}
}
   
if(n > 2) {
primeFactors.push(n);
}
   
console.log(primeFactors.join(' '));
}
getPrimeFactors(600851475143);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment