Multiples of 3 or 5
Find the sum of all natural numbers below a given limit that are multiples of 3 or 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.
How to Solve
Iterate from 1 to limit - 1 and add each number that is divisible by 3 or 5. Alternatively, use inclusion–exclusion: sum multiples of 3, plus multiples of 5, minus multiples of 15 (counted twice).
Click to reveal
Example 1:
Input:
limit = 10
Output: 23
Example 2:
Input:
limit = 1000
Output: 233168
Example 3:
Input:
limit = 1
Output: 0