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

006.py
1
2
3
4
5
6
7
#!/usr/bin/env python

x = sum(range(1,101))**2
y = sum(y**2 for y in range(1,101))
n = x - y

print n

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:

1
2
3
#!/usr/bin/env/ python

print(sum(range(1,101))**2) - sum(y**2 for y in range (1,101))

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

007.py
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
#!/user/bin/env python

# Sieve of Eratosthenes from:
# http://rosettacode.org/wiki/Sieve_of_Eratosthenes#Infinite_generator_with_a_faster_algorithm
# An odds only incremental Sieve of Eratosthenes
def eratosthenes():
    """Yields the sequence of prime numbers using the Sieve of Eratosthenes."""
    yield 2; yield 3; yield 5; yield 7;
    bps = (p for p in eratosthenes())       # additional primes supply
    p = next(bps) and next(bps)             # discard 2, then get 3
    q = p * p                               # 9 - square of next prime to be put
    sieve = {}                              # into sieve dict
    n = 9                                   # the initial candidate number
    while True:
        if n not in sieve:                  # is not a multiple of previously recorded primes
            if n < q:
                yield n                     # n is prime
            else:
                p2 = p + p                  # n == p * p: for prime p, add p * p + 2 * p,
                sieve[q + p2] = p2          # with 2 * p as incremental step
                p = next(bps); q = p * p    # advance base prime and next prime to be put
        else:
            s = sieve.pop(n); nxt = n + s   # n is composite, advance
            while nxt in sieve: nxt += s    # ensure each entry is unique
            sieve[nxt] = s                  # next non-marked multiple of this prime
        n += 2                              # work on odds only
# End SoE

def nthprime(n):
    """Uses SoE to return the nth prime."""
    for i, p in enumerate(eratosthenes()):
        if i == n - 1:
            return p

print nthprime(10001)

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 $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
20
73167176531330624919225119674426574742355349194934
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 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

008s.py
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
N = []
N.append("73167176531330624919225119674426574742355349194934")
N.append("96983520312774506326239578318016984801869478851843")
N.append("85861560789112949495459501737958331952853208805511")
N.append("12540698747158523863050715693290963295227443043557")
N.append("66896648950445244523161731856403098711121722383113")
N.append("62229893423380308135336276614282806444486645238749")
N.append("30358907296290491560440772390713810515859307960866")
N.append("70172427121883998797908792274921901699720888093776")
N.append("65727333001053367881220235421809751254540594752243")
N.append("52584907711670556013604839586446706324415722155397")
N.append("53697817977846174064955149290862569321978468622482")
N.append("83972241375657056057490261407972968652414535100474")
N.append("82166370484403199890008895243450658541227588666881")
N.append("16427171479924442928230863465674813919123162824586")
N.append("17866458359124566529476545682848912883142607690042")
N.append("24219022671055626321111109370544217506941658960408")
N.append("07198403850962455444362981230987879927244284909188")
N.append("84580156166097919133875499200524063689912560717606")
N.append("05886116467109405077541002256983155200055935729725")
N.append("71636269561882670428252483600823257530420752963450")

s = []
for k in N:
    for l in list(k):
        s.append(l)

n = 0
for i in xrange(0, len(s)-13):
    p = 1
    for j in xrange(i,i+13):
        p = p * int(s[j])
        if p > n:
            n = p

print n

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, $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.

009s.py
1
2
3
4
5
6
7
8
9
10
#!/usr/bin/env python

def pythagtrips():
    for a in range(1, 500 + 1):
        for b in range(1, 500 + 1):
            c = 1000 - b - a
            if c * c == a * a + b * b:
                return a * b * c

print pythagtrips()

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

010.py
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
#!/usr/bin/env python

import itertools

# Sieve of Eratosthenes from:
# http://rosettacode.org/wiki/Sieve_of_Eratosthenes#Infinite_generator_with_a_faster_algorithm
# An odds only incremental Sieve of Eratosthenes
def eratosthenes():
    """Yields the sequence of prime numbers using the Sieve of Eratosthenes."""
    yield 2; yield 3; yield 5; yield 7;
    bps = (p for p in eratosthenes())       # additional primes supply
    p = next(bps) and next(bps)             # discard 2, then get 3
    q = p * p                               # 9 - square of next prime to be put
    sieve = {}                              # into sieve dict
    n = 9                                   # the initial candidate number
    while True:
        if n not in sieve:                  # is not a multiple of previously recorded primes
            if n < q:
                yield n                     # n is prime
            else:
                p2 = p + p                  # n == p * p: for prime p, add p * p + 2 * p,
                sieve[q + p2] = p2          # with 2 * p as incremental step
                p = next(bps); q = p * p    # advance base prime and next prime to be put
        else:
            s = sieve.pop(n); nxt = n + s   # n is composite, advance
            while nxt in sieve: nxt += s    # ensure each entry is unique
            sieve[nxt] = s                  # next non-marked multiple of this prime
        n += 2                              # work on odds only
# End SoE

print sum(itertools.takewhile(lambda x: x < 2e6, eratosthenes()))

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).


  1. From this Python Cookbook excerpt.