Category Archives: Project Euler

Solving Project Euler: Problem 063

Problem 63 is an easy one. Let’s get right to it.

The 5-digit number, 16807=75, is also a fifth power. Similarly, the 9-digit number, 134217728=89, is a ninth power.

How many n-digit positive integers exist which are also an nth power?

Nothing that cannot be solved in one (word-wrapped) line of Python code.

Solving Project Euler: Problem 062

Problem 62 is yet another wonderful illustration of the limits of brute force approaches. Here is the problem description.

The cube, 41063625 (3453), can be permuted to produce two other cubes: 56623104 (3843) and 66430125 (4053). In fact, 41063625 is the smallest cube which has exactly three permutations of its digits which are also cube.

Find the smallest cube for which exactly five permutations of its digits are cube.

On the face of it, the approach seems evident: list out all cubes up to a certain upper bound. For each cube, check if the list of cubes has at least 4 other numbers formed by transposing its digits. The lowest number in that set is the result.

I will admit to taking this approach initially. And my program ran and ran and ran… and ran some more before I terminated it.

Why did this seemingly straightforward approach fail?

It failed because of the number of executions. To give you an idea, 1233 is 1860867. The number of permutations of this number is 5040, the factorial of the number of digits. So, just to loop through all permutations of cubes from 1 to 500, we would have over 23.3 million permutations to search through. If our upper bound was 1000 cubes, then the number shoots up to over 204 million permutations!

How can we solve this? Well, it turns out that if we changes the order of steps in our original approach, we can solve this problem in O(n) time. As we generate cubes, we can cache the digits of the cube. Any new cube that we add to our cache will be checked to see if it can be formed by an existing set of digits. Once the number of cubes for a particular set of digits reaches five, we have our answer.

Solving Project Euler: Problem 061

Problem 61 deals with figurate numbers. Here is the problem statement.

Triangle, square, pentagonal, hexagonal, heptagonal, and octagonal numbers are all figurate (polygonal) numbers and are generated by the following formulae:

Triangle P3,n=n(n+1)/2 1, 3, 6, 10, 15, …
Square P4,n=n2 1, 4, 9, 16, 25, …
Pentagonal P5,n=n(3n−1)/2 1, 5, 12, 22, 35, …
Hexagonal P6,n=n(2n−1) 1, 6, 15, 28, 45, …
Heptagonal P7,n=n(5n−3)/2 1, 7, 18, 34, 55, …
Octagonal P8,n=n(3n−2) 1, 8, 21, 40, 65, …

The ordered set of three 4-digit numbers: 8128, 2882, 8281, has three interesting properties.

  1. The set is cyclic, in that the last two digits of each number is the first two digits of the next number (including the last number with the first).
  2. Each polygonal type: triangle (P3,127=8128), square (P4,91=8281), and pentagonal (P5,44=2882), is represented by a different number in the set.
  3. This is the only set of 4-digit numbers with this property.

Find the sum of the only ordered set of six cyclic 4-digit numbers for which each polygonal type: triangle, square, pentagonal, hexagonal, heptagonal, and octagonal, is represented by a different number in the set.

The statement at the end — “only ordered set — threw me off a bit, because I took it to mean that the six numbers would be triangular, square, pentagonal and so on in that order. There is no such set, and so I had to go back to the drawing board again.

It was then that point 2 in the problem statement dawned on me. 8128, 2882, and 8281 are cyclical, but 2882 is pentagonal. This makes the problem more interesting. The set of six numbers could be any permutation of the six types of figurates we are dealing with. Thankfully, since we know that there exists only one such set of six numbers, we can stop executing when we reach that set.

Solving Project Euler: Problem 058

We encountered number spirals previously in problem 28, and now we revisit them in Problem 58. Here is the problem description.

Starting with 1 and spiralling anticlockwise in the following way, a square spiral with side length 7 is formed.

37 36 35 34 33 32 31
38 17 16 15 14 13 30
39 18  5  4  3 12 29
40 19  6  1  2 11 28
41 20  7  8  9 10 27
42 21 22 23 24 25 26
43 44 45 46 47 48 49

It is interesting to note that the odd squares lie along the bottom right diagonal, but what is more interesting is that 8 out of the 13 numbers lying along both diagonals are prime; that is, a ratio of 8/13 ≈ 62%.

If one complete new layer is wrapped around the spiral above, a square spiral with side length 9 will be formed. If this process is continued, what is the side length of the square spiral for which the ratio of primes along both diagonals first falls below 10%?

We will take the same approach here as we did for solving problem 28. For each new square layer that is added, the numbers at the four corners will be of the form n2, n2 – (n-1), n2 – (n-1)*2, n2 – (n-1)*3. We will therefore check if each of these values (except, of course, n2) is prime and increment a counter accordingly.

My first attempt at solving this problem was using a prime sieve, but even with an upper limit of 100,000,000, we will be unable to reach 10%. So my revised attempt makes use of our trusty old is_prime() function.

Solving Project Euler: Problem 057

Problem 57 is titled Square root convergents. Here is the problem statement.

It is possible to show that the square root of two can be expressed as an infinite continued fraction.

√ 2 = 1 + 1/(2 + 1/(2 + 1/(2 + … ))) = 1.414213…

By expanding this for the first four iterations, we get:

1 + 1/2 = 3/2 = 1.5
1 + 1/(2 + 1/2) = 7/5 = 1.4
1 + 1/(2 + 1/(2 + 1/2)) = 17/12 = 1.41666…
1 + 1/(2 + 1/(2 + 1/(2 + 1/2))) = 41/29 = 1.41379…

The next three expansions are 99/70, 239/169, and 577/408, but the eighth expansion, 1393/985, is the first example where the number of digits in the numerator exceeds the number of digits in the denominator.

In the first one-thousand expansions, how many fractions contain a numerator with more digits than denominator?

I found putting pen to paper very helpful in solving this problem. Obviously, trying construct a fraction going to 1000 levels should be enough to scare anyone from trying to solve this using a brute force approach. Let us write down the first few numbers in this series.

  • 3/2
  • 7/5
  • 17/12
  • 41/29
  • 99/70
  • 239/169

My first thought was that the numerators are prime, but seeing 99 in the list blew a hole in that notion. As we continue to look for patterns, we find that the next denominator in the series is merely the sum of the numerator and the denominator of the current number.

5 = 3 + 2; 12 = 7 + 5; 29 = 17 + 12, and so on

The numerators also follow a pattern. If you look at 3, 7, 17, 41, you will notice that the next numerator in the series is twice the current numerator plus the previous numerator.

17 = 7*2 + 3; 41 = 17*2 + 7; 99 = 41*2 + 17, and so on

Now that we have reduced the nth number in the series into its constituent parts, we can set up two lists, one each for the numerator and the denominator, and count for those instances where the numerator has more digits than the denominator.

Solving Project Euler: Problem 056

Problem 56 is titled Powerful digit sum. The problem statement is as follows.

A googol (10100) is a massive number: one followed by one-hundred zeros; 100100 is almost unimaginably large: one followed by two-hundred zeros. Despite their size, the sum of the digits in each number is only 1.

Considering natural numbers of the form, ab, where a, b < 100, what is the maximum digital sum?

Once you break this problem down to its constituent parts, it becomes very easy to solve.

Solving Project Euler: Problem 055

Problem 55 introduces us to Lychrel numbers. Let’s take a look at the problem statement.

If we take 47, reverse and add, 47 + 74 = 121, which is palindromic.

Not all numbers produce palindromes so quickly. For example,

349 + 943 = 1292,
1292 + 2921 = 4213
4213 + 3124 = 7337

That is, 349 took three iterations to arrive at a palindrome.

Although no one has proved it yet, it is thought that some numbers, like 196, never produce a palindrome. A number that never forms a palindrome through the reverse and add process is called a Lychrel number. Due to the theoretical nature of these numbers, and for the purpose of this problem, we shall assume that a number is Lychrel until proven otherwise. In addition you are given that for every number below ten-thousand, it will either (i) become a palindrome in less than fifty iterations, or, (ii) no one, with all the computing power that exists, has managed so far to map it to a palindrome. In fact, 10677 is the first number to be shown to require over fifty iterations before producing a palindrome: 4668731596684224866951378664 (53 iterations, 28-digits).

Surprisingly, there are palindromic numbers that are themselves Lychrel numbers; the first example is 4994.

How many Lychrel numbers are there below ten-thousand?

NOTE: Wording was modified slightly on 24 April 2007 to emphasise the theoretical nature of Lychrel numbers.

It is very helpful to know that we could stop checking after 50 attempts, for if we did not have this information, this would be a never-ending quest.

In the code listing below, I get a count of all the non-Lychrel numbers under 10000, and subtract that from 10000 to get the right answer. (While the logic is right, it took me a few tries to get to the right answer, because I did not know what number to use as my lower bound. I started with 10, moved down to 1, and finally to 0 in order to get to the right answer.)

Solving Project Euler: Problem 053

Problem 53 is titled Combinatoric selections, and its description is as follows.

There are exactly ten ways of selecting three from five, 12345:

123, 124, 125, 134, 135, 145, 234, 235, 245, and 345

In combinatorics, we use the notation, 5C3 = 10.

In general,

nCr =
n! / (r!(n−r)!)
,where rn, n! = n×(n−1)×…×3×2×1, and 0! = 1.

It is not until n = 23, that a value exceeds one-million: 23C10 = 1144066.

How many, not necessarily distinct, values of  nCr, for 1 ≤ n ≤ 100, are greater than one-million?

Interestingly, this problem only looks for the number of combinations, and the formula is listed in the problem statement itself. We will make use of this and check for values of n ≥ 23. For the given upper bound of n, it is sufficient to check for values of r between 4 and n-3. This is because 100C3 = 100C97 < 1000000.

Solving Project Euler: Problem 052

After an interesting problem 51, we have another easy problem here. Here’s the description of problem 52.

It can be seen that the number, 125874, and its double, 251748, contain exactly the same digits, but in a different order.

Find the smallest positive integer, x, such that 2x, 3x, 4x, 5x, and 6x, contain the same digits.

The code listing below should be self-explanatory, so not much of a commentary is required. I did not take a guess at the answer prior to solving this, but when I saw the output, I had an “Elementary, my dear Watson” moment.

Solving Project Euler: Problem 050

Staying with prime numbers, problem 50 focuses on primes numbers that can be expressed as the sum of consecutive prime numbers. Here is the problem statement.

The prime 41, can be written as the sum of six consecutive primes:

41 = 2 + 3 + 5 + 7 + 11 + 13

This is the longest sum of consecutive primes that adds to a prime below one-hundred.

The longest sum of consecutive primes below one-thousand that adds to a prime, contains 21 terms, and is equal to 953.

Which prime, below one-million, can be written as the sum of the most consecutive primes?

Since we know the upper limit, we can use the Sieve of Eratosthenes to generate all prime numbers under 1,000,000. Once we have this list, we shall derive the sum of consecutive prime numbers starting with 2 until we reach the upper bound. We will repeat this with subsequent prime numbers, all the while keeping count of the number of successive prime numbers that make up our total.