Project Euler: #7 - 10001st prime

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 The four adjacent digits in the 1000-digit number that has the greatest product is 9 × 9 × 8 × 9 = 5832. 73167176531330624919225119674426574742355349194934969835203127745063262395783180169848018694788518438586156078911294949545950173795833195...
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 → ...

By listing the first six prime numbers: 2, 3, 5, 7, 11, and 13, we can see that the 6th prime is 13.
What is the 10001st prime number?
We all know about Prime Numbers. A number that is divisible by 1 and itself is called a Prime Number. In other words, it has only 2 factors - 1 and itself.
So we need to find the nth prime number. For example, 3rd prime number is 5, 4th prime is 7, 6th prime is 13, 10th prime is 29 and so on.
We need to find the 10001st prime number.
To find nth prime number, we need to loop through a range of numbers from 2 to Number.MAX_SAFE_INTEGER.
Increase the counter by 1 for each prime number and once we find the nth prime, we break out of the loop.
To Check for the prime number, we use the same technique as in Problem #3 Largest prime factor
const isPrime = num => {
if (num === 1) return false;
if (num === 2 || num === 3) return true;
if (num %2 === 0 || num % 3 === 0) return false;
for (let i = 5; i <= Math.ceil(num / 2); i += 6) {
if (num % i === 0 || num % (i+2) === 0) return false;
}
return true;
}
let count = 0;
for (let i = 2; i < Number.MAX_SAFE_INTEGER; i++) {
if (isPrime(i)) count++;
if (count === n) {
console.log(i);
break;
}
}
console.time("10th");
console.log(nthPrime(10)); // 29
console.timeEnd("10th"); // 10th: 0.221923828125 ms
console.time("100th");
console.log(nthPrime(100)); // 541
console.timeEnd("100th"); // 100th: 0.68701171875 ms
console.time("10001th");
console.log(nthPrime(10001)); // 104743
console.timeEnd("10001th"); // 10001th: 827.61181640625 ms
You can find my solution on GitHub 07
This particular solution was timed out in Hacker rank for 2 out of 7 test cases. So a little bit of tweaking is necessary to achieve a better time complexity.
If you have a better solution, please leave it in the comments below.
But no worries, the above code works perfectly fine for our use case, 10001th prime number.
For the other Project Euler Solutions, please follow the series Project Euler Solutions in JS.
Thank you!