Problem
Project Euler Problem #001 asks us to find the sum of all natural numbers below 1000 that are multiples of 3 or 5 .
This is a great beginner-friendly problem because it teaches one of the most important ideas in programming:
Check every number, apply a condition, and accumulate the result.
Idea
A number should be added to the total if it is divisible by 3 or 5 .
In Python, divisibility can be checked with the modulo operator:
n % 3 == 0
Enter fullscreen mode
Exit fullscreen mode
This means:
n leaves no remainder when divided by 3 .
So the condition becomes:
n % 3 == 0 or n % 5 == 0
Enter fullscreen mode
Exit fullscreen mode
Python solution
total = 0
for n in range ( 1000 ): if n % 3 == 0 or n % 5 == 0 : total += n
print ( total )
Enter fullscreen mode
Exit fullscreen mode
Explanation
We start with:
total = 0
Enter fullscreen mode
Exit fullscreen mode
Then we loop through every number below 1000 :
for n in range ( 1000 ):
Enter fullscreen mode
Exit fullscreen mode
For…