# Five More Euler Problems

In this post I will be discussing five more Project Euler problems, with their solutions in Python, an explanation of the solutions and the background mathematics behind them. The first five I discussed here.

In this post, I’ve implemented spoiler tags that look like this: This is a spoiler!. Just mouse over or touch to reveal.

## Problem #6: Sum Square Difference

The sum of the squares of the first ten natural numbers is,

12 + 22 + … + 102 = 385

The square of the sum of the first ten natural numbers is,

(1 + 2 + … + 10)2 = 552 = 3025

Hence the difference between the sum of the squares of the first ten natural >numbers and the square of the sum is 3025 − 385 = 2640.

Find the difference between the sum of the squares of the first one hundred natural numbers and the square of the sum.

This is a fairly trivial problem. We just need to find the square of the sum of the numbers 1 – 100, the sum of the squares of the numbers 1 – 100, then find the difference between the two.

#### Solution

This gives the result 25164150.

#### Explanation

On line 3 we find the square of the sum of the natural numbers between 1 and 100. On line 4 we find the sum of the squares of the natural nubmers between 1 and 100. On line 5 we find the difference between the former and latter, then print the result.

This could be done as a one-liner such as follows:

## Problem #7: 10001st Prime

By listing the first six prime numbers: 2, 3, 5, 7, 11, and 13, we can see that the 6th prime is 13.

What is the 10 001st prime number?

Now this is a little more like it. What we need is a way to generate the sequence of prime numbers up to the nth term, and a way to return the nth term in the sequence.

#### Solution

This gives us the result 104743.

### Explanation

This does require some explanation. In order to generate the sequence of primes, we use an implementation of an ancient algorithm known as the Sieve of Eratosthenes. This particular implementation is odds-only (which speeds up the process by about 25%1), along with with postponing the inclusion of the prime’s information in the sieve dictionary until that prime’s square is seen amongst the candidate numbers.

In terms of mathematics and complexity theory, this implementation is interesting, but all we care about is that it is a) it works and b) it is more efficient than a brute force approach. In a game like prime-number generation and indexing, optimisation is everything.

## Problem #8: Largest Product In A Series

The four adjacent digits in the 1000-digit number that have the greatest product are $9 × 9 × 8 × 9 = 5832$.

Find the thirteen adjacent digits in the 1000-digit number that have the greatest product. What is the value of this product?

That’s a pretty huge number. The old wording of this problem was misleading, as it used consecutive instead of adjacent. The re-write also increased the number of adjacent digits from five to thirteen.

Essentailly, we need to find a way to iterate over the intger in chunks of 13, finding the products of said chunks and then finding the maximum.

#### Solution

This gives the result 23514624000.

#### Explanation

So what’s going on here? First of all we do some list trickery in the interests of formatting over performance (as horizontal scrolling is the devil).

Then, we have out integer ready to iterate over. We create an xrange object between 0 and the length of the large integer s, minus 13. This is because the length of the sequences we are finding the product of is 13. Otherwise, we would encounter a ListError as the list index would be out of range (looking for another 13-digit sequence that doesn’t exist).

It is then just a matter of finding the product of the 13-digit sequences p and if the product is greater than 0 (in the first loop) or greater than the previous product (in all other loops), set n as that product. Once we’ve iterated over the whole integer, we print n.

## Problem #9: Special Pythagorean Triplet

A Pythagorean triplet is a set of three natural numbers, $(a < b < c)$, for which, $$a^{2} + b^{2} = c^{2}$$ For example, $3^{2} + 4^{2} = 9 + 16 = 25 = 5^{2}$.

There exists exactly one Pythagorean triplet for which $a + b + c = 1000$.

Find the product $abc$.

A little bit of a breather after #8, as all we have to do now is some basic algebra.

This gives us the result: 31875000. The three values are: a: 200 b: 375 c: 425.

#### Explanation

We start out by defining our function, then we iterate over two ranges to get values for a and b. Why is the upper bound of the range 500? We know from the problem statement that a and b must be less than c, and if either one is 500, c must be 501 at a minimum, pushing us beyond the 1000 limit. If we wanted to really squeeze all of the performance out of this program, we’d set the upper bound of the ranges to 498 (again, as $a < b < c$).

Line 6 is our check to keep us within the 1000 limit. Line 7 just checks that the values for a, b and c we have generated satisfy Pythagoras’ theorem, then we just print the product of the three values.

## Problem #10: Summation of Primes

The sum of the primes below 10 is $2 + 3 + 5 + 7 = 17$.

Find the sum of all the primes below two million.

Fortunately, in the solution to #7, we have a very efficient way to generate prime numbers. All we need to do is use the Sieve of Eratosthenes to generate the primes up to $2\times10^{6}$. But how do we implement the bounding?

#### Solution

This gives us the result 142913828922

#### Explanation

It’s our old friend takewhile! We have the Sieve of Eratosthenes as our iterable, and lambda x: x < 2e6 as our predicate, so the sieve will generate prime numbers up until $2\times10^{6} – 1$, and we just sum the total of the primes. The sieve is an extremely useful tool for any problem that requires generating prime numbers, and it is remarkably efficient (this program only took ~0.5 seconds to run).

#### Conclusion

In this post I have discussed the Project Euler problems 6 – 10, along with their solutions written in Python. These are not definitive, nor elegant solutions. All solutions can be found as seperate .py files in the project_euler repository on my Github, and will be updatØed if/when the problems change parameters. I’m also including some timing code from the Python module time, to see how fast the solutions run in the interpreter.

#### What’s next?

An indepth discussion of a more complex Euler problem, more website tweaks and checks (implementing mathjax fully, along with some CSS tweaks for my purposes).

1. From this Python Cookbook excerpt.