JavaScript Implementation of Problem 1

View source code here on GitHub!

p0001()

Project Euler Problem 1

Did this the old fashioned way, because this was before I figured out the closed form solution

Problem:

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.

Returns:

number --

 1/**
 2 * Project Euler Problem 1
 3 *
 4 * Did this the old fashioned way, because this was before I figured out the closed form solution
 5 *
 6 * Problem:
 7 *
 8 * If we list all the natural numbers below 10 that are multiples of 3 or 5, we
 9 * get 3, 5, 6 and 9. The sum of these multiples is 23.
10 *
11 * Find the sum of all the multiples of 3 or 5 below 1000.
12 *
13 * @return {number}
14 */
15exports.p0001 = function() {
16    let answer = 0;
17    for (let i = 3; i < 1000; i += 3) {
18        answer += i;
19    }
20    for (let i = 5; i < 1000; i += 5) {
21        answer += i;
22    }
23    for (let i = 15; i < 1000; i += 15) {
24        answer -= i;
25    }
26    return answer;
27};

Tags: divisibility