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
1 2 3 4 5 6 7 

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 oneliner such as follows:
1 2 3 

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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 

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 oddsonly (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 primenumber generation and indexing, optimisation is everything.
Problem #8: Largest Product In A Series
The four adjacent digits in the 1000digit number that have the greatest product are $math$9 × 9 × 8 × 9 = 5832$math$.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 2073167176531330624919225119674426574742355349194934 96983520312774506326239578318016984801869478851843 85861560789112949495459501737958331952853208805511 12540698747158523863050715693290963295227443043557 66896648950445244523161731856403098711121722383113 62229893423380308135336276614282806444486645238749 30358907296290491560440772390713810515859307960866 70172427121883998797908792274921901699720888093776 65727333001053367881220235421809751254540594752243 52584907711670556013604839586446706324415722155397 53697817977846174064955149290862569321978468622482 83972241375657056057490261407972968652414535100474 82166370484403199890008895243450658541227588666881 16427171479924442928230863465674813919123162824586 17866458359124566529476545682848912883142607690042 24219022671055626321111109370544217506941658960408 07198403850962455444362981230987879927244284909188 84580156166097919133875499200524063689912560717606 05886116467109405077541002256983155200055935729725 71636269561882670428252483600823257530420752963450
Find the thirteen adjacent digits in the 1000digit 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 rewrite 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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 

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 13digit sequence that doesn’t exist).
It is then just a matter of finding the product of the 13digit 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, $math$(a < b < c)$math$, for which, $$a^{2} + b^{2} = c^{2}$$ For example, $math$3^{2} + 4^{2} = 9 + 16 = 25 = 5^{2}$math$.
There exists exactly one Pythagorean triplet for which $math$a + b + c = 1000$math$.
Find the product $math$abc$math$.
A little bit of a breather after #8, as all we have to do now is some basic algebra.
1 2 3 4 5 6 7 8 9 10 

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 $math$a < b < c$math$).
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 $math$2 + 3 + 5 + 7 = 17$math$.
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 $math$2\times10^{6}$math$. But how do we implement the bounding?
Solution
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 

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 $math$2\times10^{6} – 1$math$, 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).