Project Euler #1: Multiples of 3 or 5 — Solution

Kevin Andrey Romero Oviedo
2 min readOct 27, 2022

--

Image of Leonhard Euler

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

  1. Define current_sum variable and set it to 0.
  2. Iterate all numbers from 1 to 1000.
  3. Divide current number by 3. If remainder is 0, add it to current_sum.
  4. Divide current number by 5. If remainder is 0, add it to current_sum.
  5. 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.

--

--

Kevin Andrey Romero Oviedo
Kevin Andrey Romero Oviedo

Written by Kevin Andrey Romero Oviedo

I am a programmer, casual gamer, full-time geek, and math enthusiast.

No responses yet