DEV Community

Irene  Njuguna
Irene Njuguna

Posted on

Project Euler solution 1

Problem 1

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.

solution

function findMultiplesSum() {
let sum = 0;

for (let i = 1; i < 1000; i++) {
if (i % 3 === 0 || i % 5 === 0) {
sum += i;
}
}

return sum;
}

Explanation:

initialize a variable sum to keep track of the sum of multiples.then loop through the numbers from 1 to 999 (excluding 1000) using a for loop. For each number,check if it is divisible by 3 or 5 using the modulus operator (%). If it is divisible,add it to the sum variable. Finally,return the sum

Top comments (0)