/ Project Euler

Multiples of 3 and 5

A modified popular interview question serves as an introduction to loops, summations and modulus.

The problem

The objective of the first project euler problem is to find the sum of all numbers, divisible by 3 or 5, less than 1000.


Problem 1 serves as an introduction to many of the concepts required for later euler problems. While many of these concepts are programatic or mathematic, they are also literal. Many problems contain words chosen specifically to eliminate ambiguity. Paying close attention to word choices is not pedantic in project euler.

The problem description

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

The key terms of this are "natural numbers", "multiples of 3 or 5" and "below 1000". This means, in short, our loop need to begin a 0 and end at 999, without including 1000.

To determine multiples or 3 and 5, we will employ the modulus. This will allow us to loop over all numbers in the aforementioned range, and check if num % 3 == 0 or num % 5 == 0. Since 3 will divide evenly into a multiple of 3, we use modulus to confirm that the remainder of num / 3 == 0.

Finally, by summating all of the valid numbers, we can obtain an answer in a very respectable timeframe.

Multiples of 3 and 5
Share this