Problem #1
Multiples of 3 and 5
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.
Problem Break Down
Natural numbers: In mathematics, the natural numbers are those used for counting and ordering. In common language, words used for counting are “cardinal numbers” and words used for ordering are “ordinal numbers”. Wiki
Note: people are ambiguous about natural numbers too. They confuse between whole numbers and natural numbers and rightly so.
Divisibility by 3: sum the digits of the number and if that sum is divisible by 3, that number is divisible by three.
number (A) 
sum of digits (B) 
(B) divisible by 3 (C) 
(A) divisible by 3 (D) 
1 
1 
no 
1 is not divisible by 3 
2 
2 
no 
2 is not divisible by 3 
3 
3 
yes 
divisible 
4 
4 
no 
4 is not divisible by 3 
5 
5 
no 
5 is not divisible by 3 
6 
6 
no 
divisible 
7 
7 
no 
7 is not divisible by 3 
8 
8 
no 
8 is not divisible by 3 
9 
9 
no 
divisible 
10 
1 
no 
10 is not divisible by 3 
11 
2 
no 
11 is not divisible by 3 
12 
3 
no 
divisible 
13 
4 
no 
13 is not divisible by 3 
14 
5 
no 
14 is not divisible by 3 
15 
6 
no 
divisible 
Note that divisibility here means whole division and not fractional result.
 Upto 15
 numbers that are divisible by 3: 3, 6, 9, 12, 15
 sum = 3 + 6 + 9 + 12 + 15 = 45
Divisibility by 5: If least significant digit is either 0 or 5, it is divisible by 5.
number (A) 
Least Significant Digit (B) 
(B) is 0 or 5 (C) 
(A) divisible by 5 (D) 
1 
1 
no 
1 is not divisible by 5 
2 
2 
no 
2 is not divisible by 5 
3 
3 
no 
3 is not divisible by 5 
4 
4 
no 
4 is not divisible by 5 
5 
5 
yes 
divisible 
6 
6 
no 
6 is not divisible by 5 
7 
7 
no 
7 is not divisible by 5 
8 
8 
no 
8 is not divisible by 5 
9 
9 
no 
9 is not divisible by 5 
10 
0 
yes 
divisible 
11 
1 
no 
11 is not divisible by 5 
12 
2 
no 
12 is not divisible by 5 
13 
3 
no 
13 is not divisible by 5 
14 
4 
no 
14 is not divisible by 5 
15 
5 
yes 
divisible 
Note that divisibility here means whole division and not fractional result.
 Upto 15
 numbers that are divisible by 5: 5, 10, 15
 sum = 5 + 10 + 15 = 30
 Sum of numbers that are divisible by 3 or divisible by 5

45 + 30 = 75
But what about numbers like 15, 30 and so on…
Well, 15 is divisible by 3 and 5. If we are counting the numbers that are divisible by 3 or 5; we have counted 15 twice above.
We need to take out the numbers that are divisible by both.
 Upto 15
 Numbers that are divisible by 3 and 5 = 15
 Sum of numbers divisible by 3 = 45
 Sum of numbers divisible by 5 = 30
 Sum of numbers divisible by 3 or 5 = 45 + 30  15 = 60
Let’s check:
3 + 5 + 6 + 9 + 10 + 12 + 15 = 60
Code
pseudo
Getting the numbers that are divisible
by a given number N
below `upto`
numbersDivisibleBy ( N, upto ) {
numberArray = [];
loop i in 1 to upto:
if i modulus N is 0:
numberArray.add(N)
return numberArray
divisibleBy3 = numbersDivisibleBy(3, 15)
divisibleBy5 = numbersDivisibleBy(5, 15)
divisibleBy15 = numbersDivisibleBy(15, 15)
result = (diff (sum (divisibleBy3, divisibleBy5), divisibleBy15)
javascript (in chrome console)
We are using arrow syntax (input => output).
const divisibleBy = (n, upto) => {
let numbers = [];
for(let i = 1; i <= upto; i++ ) {
if(i % n === 0) {
numbers.push(i);
}
}
return numbers;
}
let divisibleBy3 = divisibleBy(3, 9);
let divisibleBy5 = divisibleBy(5, 9);
let divisibleBy15 = divisibleBy(15, 9)
divisibleBy3
[3, 6, 9]
divisibleBy5
[5]
divisibleBy15
[]
Our result can be calculated as:
divisibleBy3.reduce((p, c) => p+c, 0) + divisibleBy5.reduce((p, c) => p+c, 0)  divisibleBy15.reduce((p, c) => p+c, 0)
Note that , 0
is defensive in first two instances and a necessity in last one and we are using Array.reduce
Questions still left
What if we need to find sum of numbers divisible by 3 upto Number.MAX_SAFE_INTEGER (=== 9007199254740991 in javascript)?
let divisibleBy3 = divisibleBy(3, Number.MAX_SAFE_INTEGER)
// oops, crash
What if we need to sum all such numbers? How big will the sum become?
What crashed dev tools? A fact that Arrays can keep only limited numbers in them?
and more.