Skip to content

Instantly share code, notes, and snippets.

@vasco3
Created September 18, 2013 04:52
Show Gist options
  • Save vasco3/6604683 to your computer and use it in GitHub Desktop.
Save vasco3/6604683 to your computer and use it in GitHub Desktop.
/* The prime factors of 13195 are 5, 7, 13 and 29. What is the largest prime factor of the number 600851475143 ? */
/*
The prime factors of 13195 are 5, 7, 13 and 29.
What is the largest prime factor of the
number 600851475143 ?
*/
function getPrime(n){
var aPrimes = [], max = 0;
function primeTest(d){
//test the divisor to see if it's prime
for ( var i = 2; i <= Math.floor(Math.sqrt(d)); i++ ) {
//if it's divisible it isn't prime
if( d % i === 0 ){
return false;
}
}
return true;
}
//check numbers until sqrt of n
for ( var i = 2; i <= Math.floor(Math.sqrt(n)); i++ ) {
//if it divides the n test it for primability
if( n % i === 0 ){
//if it is a prime save it
if(primeTest(i)){ aPrimes.push(i) }
}
};
console.log( aPrimes );
max = Math.max.apply(null, aPrimes );
return max;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment