Project Euler: #10 - Summation of primes

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 In the 20×20 grid below, four numbers along a diagonal line have been marked in yellow. 08 02 22 97 38 15 00 40 00 75 04 05 07 78 52 12 50 77 91 0849 49 99 40 17 81 18 57 60 87 17 40 98 43 69 48 04 56 62 0081 49 31 73 55 79 14 29 93 71 40 67 ...
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 sum of the primes below 10 is 2 + 3 + 5 + 7 = 17.
Find the sum of all the primes below two million.
The problem is pretty self-explanatory!
Sum of primes below 10 is 17.
Find the sum of primes below 2000000.
To calculate the sum of primes, we can use the brute force algorithm method, which solves a problem through exhaustion - it goes through all possible choices until a solution is found.
The brute force method is simple and consistent but very slow. It affects the time complexity of the program as it depends on the size of the input, in our case we need to loop two million times.
We loop through the numbers till 2 million and push them to an array allPrimes, if it is a prime number.
The steps involved in determining isPrime(n) is the same as we did with problem #3 - Largest Prime factor and problem #7 - 10001st Prime. Except that here, I opted for a while loop instead of for loop.
Finally, using the array reduce() method, the sum of all the primes in the array allPrimes is calculated.
const isPrime = n => {
if (n === 2) return true;
if (n === 3) return true;
if (n % 2 === 0) return false;
if (n % 3 === 0) return false;
let i = 5,
next = 2;
while (i * i <= n) {
if (n % i === 0) {
return false;
}
i += next;
next = 6 - next;
}
return true;
}
let allPrimes = [];
for (let i = 2; i <= 2000000; i++) {
if (isPrime(i)) allPrimes.push(i);
}
console.log(allPrimes.reduce((a, b) => a + b, 0));
You can find my solution on GitHub 10.
The above solution failed 3 out of 10 test cases in Hacker rank(Of course Hacker rank uses a variety of test cases from a small number to a very big number).
Hence I tweaked the solution to achieve the time complexity for passing all the test cases.
First, I adjusted a bit while calculating isPrime(n).
const isPrime = n => {
if (n === 1) return false;
if (n < 4) return true; // 2 & 3 are prime
if (n % 2 === 0) return false;
// We have excluded 4, 6 & 8 in the previous step.
if (n < 9) return true;
if (n % 3 === 0) return false;
let i = 5;
// n rounded to the greatest integer s so that s*s <= n
let s = Math.floor(Math.sqrt(n));
while (i <= s) {
if (n % i === 0) return false;
if (n % (i + 2) === 0) return false;
i = i + 6;
}
return true;
}
Note: The below code is only for the Hacker Rank solution to pass all test cases. 👇
Even after this, 2 test cases were timed out. So instead of getting the allPrimes for every test case, declare the allPrimes array as global to all test cases, push the prime numbers into this array from where we left in the last test case till we reach the current n. In this way, we won't repeat the same process again and again for each test case.
function main() {
var t = parseInt(readLine());
let allPrimes = []; // Moved this here. Common to all the input.
for(var a0 = 0; a0 < t; a0++){
var n = parseInt(readLine());
// let allPrimes = [];
let sum = 0,
temp = true;
// If allPrimes has items, get the last item to lastNum
// Else initialise lastNum to 1.
// We will loop through from lastNum till we reach n
// and add the prime numbers into the array allPrimes
let lastNum = allPrimes.length !== 0 ?
allPrimes[allPrimes.length-1] : 1;
// If lastNum is greater than n, then we already have
// the prime numbers till n in the allPrime array.
// So loop through allPrime array till <= n, and
// sum all the prime numbers
if(lastNum > n){
temp = false;
for(let l = 0 ; l < allPrimes.length ; l++ ){
if(allPrimes[l] <= n){
sum += allPrimes[l];
}
}
}
// Else, from lastNum + 1, we need to find the prime till n
lastNum++
while(lastNum <= n){
if(isPrime(lastNum)){
allPrimes.push(lastNum)
}
lastNum++
}
if(temp){
sum = allPrimes.reduce((acc,cur) => acc + cur, 0)
}
console.log(sum)
}
}
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!