Skip to content

Instantly share code, notes, and snippets.

@vlad-bezden
Created April 1, 2016 16:04
Show Gist options
  • Save vlad-bezden/97654581674361bf4d09e7cc31072350 to your computer and use it in GitHub Desktop.
Save vlad-bezden/97654581674361bf4d09e7cc31072350 to your computer and use it in GitHub Desktop.
Project Euler. Problem 3

Project Euler. Problem 3

Largest prime factor

The prime factors of 13195 are 5, 7, 13 and 29.

What is the largest prime factor of the number 600851475143 ?

A Pen by Vlad Bezden on CodePen.

License.

'option strict';
function largestPrimeFactor(val, divisor = 2) {
let square = (val) => Math.pow(val, 2);
while ((val % divisor) != 0 && square(divisor) <= val) {
divisor++;
}
return square(divisor) <= val
? largestPrimeFactor(val / divisor, divisor)
: val;
}
console.log(largestPrimeFactor(600851475143));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment