Project Euler: #3 - Largest prime factor

Search for a command to run...

No comments yet. Be the first to comment.
In this series, I will be discussing Project Euler Solutions. 'Project Euler' helps in improving problem-solving and programming skills. Remember, there is always a better solution to the problem. 💯
Problem A palindromic number reads the same both ways. The largest palindrome made from the product of two 2-digit numbers is 9009 = 91 × 99. Find the largest palindrome made from the product of two 3-digit numbers. Problem Description A palindromic...
What is JavaScript? Discover how this quirky yet powerful language runs in browsers, what makes it tick, and how you can start adding it to your sites

Problem If the numbers 1 to 5 are written out in words: one, two, three, four, five, then there are 3 + 3 + 5 + 4 + 4 = 19 letters used in total. If all the numbers from 1 to 1000 (one thousand) inclusive were written out in words, how many letters w...

Problem 215 = 32768 and the sum of its digits is 3 + 2 + 7 + 6 + 8 = 26. What is the sum of the digits of the number 21000? Problem Description The problem is pretty self-explanatory. 215 is 32768 and the sum of its digits is 3 + 2 + 7 + 6 + 8 = 26....

Problem Starting in the top left corner of a 2*2 grid, and only being able to move to the right and down, there are exactly 6 routes to the bottom right corner. How many such routes are there through a 20*20 grid? Problem Description We are given a...

Problem The following iterative sequence is defined for the set of positive integers: n → n/2 (n is even) n → 3n + 1 (n is odd) Using the rule above and starting with 13, we generate the following sequence: 13 → 40 → 20 → 10 → 5 → 16 → 8 → 4 → 2 → ...

The prime factors of 13195 are 5, 7, 13 and 29.
What is the largest prime factor of the number 600851475143?
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.
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 max to the lowest number possible. max = 0
Step-2 - While the number n is divisible by 2, then assign max = 2 and divide n by 2. Continue this while loop till the number n is divisible by 2. In this step, we remove all possible even factors.
Step-3 - Once the while loop(in step-2) finishes, n will be odd. Now while n is divisible by 3, then assign max = 3 and divide n by 3. Continue this while loop till the number n is divisible by 3. In this step, we remove all possible 3 factors
Step-4 - We loop over all the possible odd factors and update the max to the largest prime factor. Now start a loop from i = 5 to the square root of n.
Step-4a - While i divides n, assign max = i, and divide n by i. After i fails to divide n, end this while
Step-4b: While i + 2 divides n, assign max = i + 2, and divide n by i + 2. After i + 2 fails to divide n, end this while
After both while, increment i by 6 and continue. In this way, we will iterate only for integers that do not have prime factors 2 and 3
Check if n greater than 4. If so, then that would be the largest prime number factor or else max will be the largest prime factor.
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.