Problem 1: Multiples of 3 or 5¶
Link to the problem: https://projecteuler.net/problem=1.
Problem 1
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.
A simple solution¶
A simple way to do it, is to go through all numbers from \(1\) to \(999\) and test whether they are divisible by either \(3\) or \(5\).
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | |
[info] [2023-02-24 14:01:39.193] [T-4856] [logging.cpp::62] [main-wire] Answer: 233168
A better and faster solution¶
If we are to do the same for all numbers less than \(100,000,000\) that is going to take a while.
We can use the fact that the sum of all numbers between \(1\) and \(p\) can be directly calculated with the following formula:
Adding together all numbers between \(1\) and \(n\) divisible by \(3\) is the same as calculating that sum for all numbers between \(1\) and \(n/3\) then multiplying the result by \(3\).
The same applies for the numbers divisible by \(5\). Then we also need to calculate the same for all numbers that are both divisible by \(3\) and \(5\) (i.e. divisible by \(15\)), as they would otherwise be counted twice.
In the end, our calculation is:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | |
[info] [2023-02-24 14:01:39.271] [T-6048] [logging.cpp::62] [main-wire] Answer: 2333333316666668
More details on the Project Euler's website: https://projecteuler.net/overview=001.