Project Euler: #1 - Multiples of 3 or 5

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 Each new term in the Fibonacci sequence is generated by adding the previous two terms. By starting with 1 and 2, the first 10 terms will be: 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, ... By considering the terms in the Fibonacci sequence whose value...
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 → ...

If we list all the natural numbers below 10 that are multiples of 3 or 5, we get 3, 5, 6 and 9. The sum of these multiples is 23.
Find the sum of all the multiples of 3 or 5 below 1000.
This is a more straightforward problem.
The natural numbers below 10 would be 1,2,3,4,5,6,7,8 and 9 (Since it is below 10, 10 is exclusive)
And those multiples of 3 are 3, 6 and 9
And multiples of 5 would be 5.
So now, the sum of all those multiples would be,
3 + 6 + 9 + 5 = 23.
So now, we want to find the sum of all the multiples of 3 and 5 below 1000 (1000 is exclusive)
My approach is to get all the multiples of 3 or 5 in an array - multiples and then use Array.prototype.reduce() function to calculate the sum of all items in that array multiples
For my solution, the time taken is between 0.11ms and 0.14ms.
Another approach can be to add the multiples directly to a variable, instead of pushing to an array. Inside the if, we can use sum += i and print the sum which is our result.
As I said before, there is more than one approach to each problem!
const multiples = [];
for (let i = 1; i < 1000; i++) {
if (i % 3 === 0 || i % 5 === 0) {
multiples.push(i);
}
}
console.time("p#1");
console.log(multiples.reduce((acc, curVal) => acc + curVal));
console.timeEnd("p#1");

The GitHub repo for this problem is found at 01
If you have another 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!