Regular Memory

Use Python to Answer Question One Project Euler

Posted at — Nov 23, 2019

Answering challenging questions can be fun and rewarding, and programming languages allow us to translate our solution ideas into a language the computer can understand. Once we explain to a computer the steps involved in solving the question, the computer will calculate the answer for us. In this article, I explain how to install the Python programming language on your local machine, and write code that solves Question One on Project Euler.

Python: Python is an interpreted, object-oriented, high-level programming language with dynamic semantics.

Project Euler: A collection of math and computing problems that start easy and increase in level.

Installing Python

You can download and install Python on your operating system by visiting the Python download page. If you have difficulty, many online guides can be found that explain how to install Python.

Question One - Project Euler

Visit the the first question on Project Euler here. If you make an account, you’ll be able to submit an answer for verification. Here’s what the question wants us to do:

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.

We need to look at every number from 1 to 999, take the ones that are multiples of 3 or 5, and add them together. The numbers are small enough that we could do this by hand, or even in our head possibly if we knew some addition formulas! But, this question will be a good opportunity to learn coding with Python.

Writing Python Code to Answer Question One

The Python code we need to answer the question is shown below. I’ve added comments before each line. Below, I give a line by line walkthrough of what the code does:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
# A variable to store our answer
ans = 0

# Loop over the numbers from 0 to 999
for n in range(1000):

    # If the remainder of the division
    # of n by 3 or 5 is zero then its a
    # multiple and we add n to the answer
    if (n % 3) == 0 or (n % 5) == 0:
        ans += n

# Print the answer, so we can input it to the site
print("ans:", ans)

In the code block above, on line 2, we declare an integer variable called ans to store our answer. On line 5, we begin a loop. The code inside the loop on lines 6 to 11 will run 1000 times. Each time the code is run the variable n will be incremented by one. n starts at 0 and will go all the way to 999 (we don’t want to include the number 1000). Inside the loop, on line 10, we check if n is a multiple of 3 or 5. This statement might look a bit complicated, so let’s break it down:

(n % 3) == 0

The statement above calculates n mod 3 where mod is replaced by %. To learn more about what mod does read this. Basically, n % 3 means divide n by 3 and give us the remainder. We then ask if the remainder is equal to zero. If the remainder is equal to zero, we know that the given value of n is a multiple of 3 (or 5). Only if the condition is true, we execute line 14 which adds n to our answer. Finally, on line 14, we print the total value that we found by adding all multiples of 3 and 5 from 1 to 999 (inclusive).

Final Steps

Running the Python code above gives us the answer we need to submit to Project Euler. Submitting the answer completes the question and lets us move onto question two. This article covers only a few of Python's many capabilities. Check out the Python Challenge to see what else Python can do!

comments powered by Disqus