Christopher Tobin

Project Euler Problem 1 Analysis

Christopher Tobin
Christopher Tobin

November 01, 2019

The very first problem in the famous Project Euler and its a good one! This is a great way to get our feet wet by creating a program to solve a mathematical problem.

Multiples of 3 and 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.

What is being asked

In order to properly answer this problem (or any problem really), we must first understand what is being asked.

For this problem, we are being asked to find the sum of all of the numbers that are multiples of either 3 OR 5. That’s a pretty straightforward ask. The next part can be a bit less clear though, there is some ambiguity with the phrase below 1000. With these types of phrases it is sometimes difficult to establish whether the number 1000 should be included. There are two things that give us some insight into whether to include 1000 in the list of potential numbers:

  • The example uses the same wording when establishing the boundaries for the numbers - it asks for multiples below 10. The answer to the example clearly does not include the number 10 because 5 is a multiple of 10.
  • The wording below 1000 indicates that the numbers 1 through 999 should be considered. Because the number 1000 is not below 1000.

Approach

Now that we know what is being asked, our next objective is to determine all of the numbers up to, but not including, 1000 that are divisible by either 3 or 5.

For a number to be divisible by another number, there must be no remainder left after the division is completed. For example, if we divide the number 3 by the number 2, the remainder will be 1. In this example, 3 is not divisible by 2.

If we really wanted to we could solve this problem without writing any code. But what fun is that??

The mod / % operator would be very useful here, as that will tell us whether one number can be divided evenly into another number.

Solution

Coming soon…