Project Euler: #3 - Largest prime factor

Problem
The prime factors of 13195 are 5, 7, 13 and 29.
What is the largest prime factor of the number 600851475143?
Problem Description
If any given number, N is divisible by x without leaving a reminder, then x is a Factor of the Number N.
Similarly, a Prime Number is a number that is divisible by 1 and itself. In other words, it has only 2 factors - 1 and itself.
Well, 10 is not a prime number as it has other factors like 2 and 5. And 2 is the only even prime number we have and all the other prime numbers are odd. 1 is neither a prime nor composite number.
So, in our problem, we need to find the Largest Factor of a given number which is also a prime.
Given 13195, the prime factors are 5, 7, 13 and 29. Here 29 is our largest prime factor.
We need to find the Largest prime factor for 600851475143.
Approach
The approach here is to factorize the given number with a divisor starting from 2 and update the max with the divisor.
Here is how it is approached,
Step: 1 - Initialise
maxto the lowest number possible.max = 0Step-2 - While the number
nis divisible by 2, then assignmax = 2and dividenby2. Continue thiswhileloop till the numbernis divisible by2. In this step, we remove all possible even factors.Step-3 - Once the while loop(in step-2) finishes,
nwill be odd. Now whilenis divisible by 3, then assignmax = 3and dividenby3. Continue thiswhileloop till the numbernis divisible by3. In this step, we remove all possible3factorsStep-4 - We loop over all the possible
oddfactors and update themaxto the largest prime factor. Now start a loop fromi = 5to the square root ofn.Step-4a - While
idividesn, assignmax = i, and dividenbyi. Afterifails to dividen, end thiswhileStep-4b: While
i + 2dividesn, assignmax = i + 2, and dividenbyi + 2. Afteri + 2fails to dividen, end thiswhileAfter both
while, incrementiby6and continue. In this way, we will iterate only for integers that do not have prime factors2and3
Check if
ngreater than4. If so, then that would be the largest prime number factor or elsemaxwill be the largest prime factor.
Solution
function largestPrimeFact(n) {
let max = 0;
while(n % 2 == 0) {
n /= 2; // Equivalent to n = n/2 OR n >>= 1(Right shift assignment)
max = 2;
}
while(n % 3 == 0) {
n /= 3; // Equivalent to n = n/3
max = 3;
}
for (let i = 5; i <= Math.sqrt(n); i += 6) {
while (n % i == 0) {
max = i;
n /= i; // Equivalent to n = n/i
}
while (n % (i + 2) == 0) {
maxPrime = i + 2;
n = n / (i + 2);
}
}
return n > 4 ? n : max;
}
console.time("L2");
console.log(largestPrimeFact(600851475143)); // 6857
console.timeEnd("L2"); // L2: 0.155029296875 ms
I have another approach here on GitHub, using a straightforward while loop solution and it throws a timeout error for a very big number on HackerRank. You can find the code here 03.
If you have another or a better solution, please leave it in the comments below.
For the other Project Euler Solutions, please follow the series Project Euler Solutions in JS.
Thank you!
Note:
I have created a sheet with the iterations for both approaches here.






