Project Euler #1: Multiples of 3 or 5 — Solution
Project Euler is a series of challenging mathematical/computer programming problems that will require more than just mathematical insights to solve. Although mathematics will help you arrive at elegant and efficient methods, the use of a computer and programming skills will be required to solve most problems.
Read more about Project Euler here.
Problem 1: 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.
To determine if a number is a multiple of 3 or 5, we have to divide the number by 3 or 5 respectively, and if the remainder is 0 then the number is multiple of 3 or 5.
For example: 8 divided by 3 is 2.67, and 8 divided by 5 is 0.4, which means that 8 is not multiple of 3 or 5.
Solution: Iterate Numbers
- Define current_sum variable and set it to 0.
- Iterate all numbers from 1 to 1000.
- Divide current number by 3. If remainder is 0, add it to current_sum.
- Divide current number by 5. If remainder is 0, add it to current_sum.
- After checking all numbers, display current_sum.
Code
Disclaimer
The information you see here is intended for educational-use only. It is my own interpretation of the problem and solution, and there may be other efficient solutions.