Problem
Find the sum of all the multiples of 3 or 5 below 1000. e.g. 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.
Solutions
We implement two solutions but first I want to mention common function in both solutions...
const isMultiple = (num, mode) => (num % mode ? false : true);
// Checks the given number is multiple or not with the help of modulus
// e.g. isMultiple(6, 3) returns true because 6 % 3 remainder is 0.
// This mean 6 is multiple of 3.
// At the other hand isMultiple(26, 5) returns false
With => Array Methods
1.
// initial array range/length
const numRange = 1000;
// Empty Array with 1000 undefined items
const array1000 = [...Array(numRange)];
2.
// array where we push 3 or 5 multiples
const threeOrFiveMultiples = [];
// common util function for checking multiples
const isMultiple = (num, mode) => (num % mode ? false : true);
// mapping over an undefined items array and pushing
// 3 or 5 multiples indexes to threeOrFiveMultiples array
array1000.map((_, index) => {
if (isMultiple(index, 3) || isMultiple(index, 5))
threeOrFiveMultiples.push(index);
});
3.
// reducing the array to a single value
const sum = threeOrFiveMultiples.reduce((acc, curr) => {
return acc + curr;
}, 0);
// logs the sum of all the multiples of
// 3 or 5 below 1000 => 233168
console.log(sum);
4.
// complete solution with array methods
// initial array range/length
const numRange = 1000;
// Empty Array with 1000 undefined items
const array1000 = [...Array(numRange)];
// array where we push 3 or 5 multiples
const threeOrFiveMultiples = [];
// common util function for checking multiples
const isMultiple = (num, mode) => (num % mode ? false : true);
// mapping over an undefined items array and pushing
// 3 or 5 multiples indexes to "threeOrFiveMultiples" array
array1000.map((_, index) => {
if (isMultiple(index, 3) || isMultiple(index, 5))
threeOrFiveMultiples.push(index);
});
// reducing the array to a single value
const sum = threeOrFiveMultiples.reduce((acc, curr) => {
return acc + curr;
}, 0);
// logs the sum of all the multiples of
// 3 or 5 below 1000 => 233168
console.log(sum);
With => For Loop
1.
let sum = 0;
// loop max range
const numRange = 1000;
// common util function for checking multiples
const isMultiple = (num, mode) => (num % mode ? false : true);
2.
// looping over an numbers below 1000 and adding 3 or 5
// multiples to sum
for (let i = 0; i < numRange; i++) {
if (isMultiple(i, 3) || isMultiple(i, 5)) sum += i;
}
// logs the sum of all the multiples of
// 3 or 5 below 1000 => 233168
console.log(sum);
3.
// complete solution with For Loop
let sum = 0;
// loop max range
const numRange = 1000;
// common util function for checking multiples
const isMultiple = (num, mode) => (num % mode ? false : true);
// looping over an numbers below 1000 and adding 3 or 5
// multiples to sum
for (let i = 0; i < numRange; i++) {
if (isMultiple(i, 3) || isMultiple(i, 5)) sum += i;
}
// logs the sum of all the multiples of
// 3 or 5 below 1000 => 233168
console.log(sum);
Let me know which solution you like more and why? (BTW my favorite one is with "For Loop" because it's so easy and less code)
And, for more cool stuff, follow me on Twitter @NomanGulKhan and GitHub @NomanGul
Top comments (3)
This can be solved way more efficiently without using arrays or loops, if we approach it mathematically.
Let's assume:
a
b
c
d
then, clearly,
a = b + c - d
To find,
a
, we can start with finding the values ofb
,c
andd
.To solve this, let's write find an algorithm to find the multiples of
x
belowy
.x
(strictly) belowy
isn = Math.floor((y-1) / x)
x
is alwaysx
.x
belowy
form an Arithmetic Progression (AP),x, 2x, 3x ... nx
s = x + 2x + 3x + ... + nx
=> s = x * (1 + 2 + 3 + ... + n )
=> s = x * n * (n + 1) / 2
Let's write a function for that,
This way, we solve the problem in
O(1)
time and space complexity.My solution:
// 233168
Woooah... š³ Are you Einstein?