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.
http://projecteuler.net/problem=1
Brute Force
To brute force an answer, we check each number less than 1000 is a multiple of 3 or 5, adding it to the sum if it passes the check.
Another way to brute force the problem is to add all the multiples of 3 and multiples of 5 then subtract the multiples of both 3 and 5. The last step is necessary because the multiples of 15 have been added sum twice.
This method ends up being less performant than our initial approach but is a step in the right direction.
Arithmetic Progressions and Series
An arithmetic progression
is a sequence when the difference between two consecutive members is constant delta.
An arithmetic series
is the sum of all the members a finite arithmetic progression.
It can be generalized as:
Having this information, we can replace the loop in our previous solution with a function that returns the sum.