// you’re reading...
1 Star2 Stars3 Stars4 Stars5 Stars (20 votes, average: 5.00 out of 5)

Project Euler Solutions

Project Euler 1 Solution

Project Euler 1 Solution

Project Euler 1: Find the sum of all the multiples of 3 or 5

Project Euler 1 Problem Description

Project Euler 1: 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.


This problem wants us to find the sum of all the natural numbers (positive integers) below 1000 that are evenly divided by 3, such as 3, 6, 9, … and 5, such as 5, 10, 15, …

Obvious solution

This can be implemented quickly and intuitively by using an iterative approach that loops through a range of integers between 1 and 999. Using the mod operator to check for even divisibility (a 0 remainder after division) we sum those integers, i, that are divisible by 3 or 5.

# single line using list comprehensions in python
print sum(i for i in xrange(1, 1000) if i%3==0 or i%5==0)

# a classical way in python
s = 0
for i in xrange(1, 1000):
	if i%3==0 or i%5==0: s+= i
print s

The program runs instantly for the less than 1000 limit but we quickly learn that this method does not scale to larger ranges such as 1 billion run 100,000 times is less than a second. Such is the demand required to be met by the HackerRank Project Euler 1. So we need to find a more efficient way of calculating this sum without looping.

Scalable solution

There is a formula, attributed to Carl Friedrich Gauss, that will calculate the sum of the first n natural numbers and, by extension, the first nth natural numbers where nth will be 3 and 5 respectively.

\sum\limits_{i=1}^n i = \frac{n(n+1)}{2}

For example, when n=10 the sum of all the natural numbers from 1 through 10 is: (1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 10) = 10*11 / 2 = 55.

Here’s how this formula works. Write the numbers in two rows that wrap around as shown below:

 1  2  3  4  5
10  9  8  7  6

The sum of each column is 11 (i.e., n+1). As the top row increases, the bottom row decreases, so the column sum always stays the same and we’ll always have 2 rows and n/2 columns for any even number, n. If n is odd, start with zero instead of one.

\displaystyle{\text{Columns * Column sum} = (\frac{n}{2})(n+1) = \frac{n(n+1)}{2}}

which is the same formula as above.

We can adapt this formula to count the numbers only divisible by d to a specific limit; such as n=33, d=3, in the following example. Remember when there is an odd number we start from zero to keep the columns paired. Here’s how the adaptation works:

 0  3   6   9  12  15
33 30  27  24  21  18

Each column sums to 33 and, using our understanding from above, we calculate 6*33 to find the sum of numbers to 33 that are evenly divisible by 3 (6*33=198). In the Python function sumn this is accomplished by taking the floor of n divided by d to find the number of terms, then calculate the sum using the expanded formula below which accounts for the multiplier d.

\displaystyle{\sum\limits_{i=1}^m id = d\hspace{1mm}\frac{m(m+1)}{2}}

By applying the above formula to n=999 and d=3 and d=5 we get the sums for every 3rd and 5th number. Adding those together is almost our answer but we must first subtract the sum of every 15th natural number (3 × 5) as it is counted twice: once in the 3 summation and once again in the 5 summation.

This is a typical application of the inclusion-exclusion principle. In general, sum the numbers less than 1000 that are divisible by 3 (3, 6, 9, 12, 15, …) or 5 (5, 10, 15, …) and subtract those divisible 3 and 5 (15, 30, 45, …).

This solution is much faster than using brute force which require loops, especially when the limit, L, is large. Also note that we subtract 1 from the limit as the problem wants multiples below L.

Project Euler 1
This program and method
solves all test cases for
Project Euler 1 on HackerRank

Project Euler 1 Solution

Runs < 0.001 seconds in Python 2.7.
download arrowUse this link to get the Project Euler 1 Solution Python 2.7 source.


Slowly swipe from either end beginning with the white vertical bar to get an idea of the starting or ending digits. For less drama, just double click the answer area. The distance between the two bars will give you an idea of the magnitude. Touch devices can tap and hold the center of the box between the two bars and choose define to reveal the answer.


Bendixen_-_Carl_Friedrich_Gauß,_1828, Project Euler 1The formula is the legacy of Carl Friedrich Gauss, the German mathematician. Here’s how he figured it out:

The sequence of numbers [1, 3, 6, 10, 15,…] are called the triangular numbers. The game of bowling or ten-pin sets 10 pins in a triangular form and thus uses the fourth number in this sequence. One pin in the first row and ending with 4 pins in the last row.

To calculate the Nth triangular number you add the first N numbers: 1 + 2 + 3 + … + N. If you want to find the 100th triangular number, you begin the long and laborious addition of the first 100 numbers.

Indeed, Gauss’s teacher liked to assign these kinds of problems to his class as it would keep them busy and quiet for some time. As each student finished the task they would place their slate tablets with their answer written on it in a pile in front of the teacher. While the other students began laboring away, the ten-year-old Gauss had laid his tablet on the table within seconds. Dubious, the teacher thought the young Gauss was being lazy. But when he looked at Gauss’s slate, there was the answer — 5,050 — with no steps in the calculation. The teacher thought that Gauss must have cheated somehow, but the pupil explained that all you needed to do was put N=100 into the formula 1/2 × (N + 1) × N and you will get the 100th number in the list without further additions.

Rather than tackling the problem head on, Gauss had thought geometrically. He argued that the best way to discover how many beans there were in a triangle with 100 rows was to take a second similar triangle of beans which could be placed upside down on top of the first triangle. Now Gauss had a rectangle with 101 rows each containing 100 beans. Calculating the number of beans in this rectangle built from the two triangles was easy: there are in total 101 × 100 = 10,100 beans. So one triangle must contain half this number, namely 1/2 × 101 × 100 = 5,050.

Example for 5 rows

x                 o      x o o o o o
x x             o o      x x o o o o
x x x         o o o  —>  x x x o o o
x x x x     o o o o      x x x x o o
x x x x x o o o o o      x x x x x o

Project Euler 1

Project Euler 1 Solution last updated



  1. […] Solving this problem requires the knowledge of two established formulas: 1. The sum of the first n numbers (triangular numbers, used in Project Euler Problem 1): […]

Post a comment