Monthly Archives: August 2016

Solving Project Euler: Problem 040

We are introduced to Champernowne’s constant in problem 40. Here is the problem statement.

An irrational decimal fraction is created by concatenating the positive integers:

0.123456789101112131415161718192021…

It can be seen that the 12th digit of the fractional part is 1.

If dn represents the nth digit of the fractional part, find the value of the following expression.

d1 × d10 × d100 × d1000 × d10000 × d100000 × d1000000

Applying Occam’s razor to this problem, we can solve it as follows.

Solving Project Euler: Problem 039

Problem 39 deals with finding the sum of a Pythagorean triad that has the most number of distinct values for a, b, and c.

If p is the perimeter of a right angle triangle with integral length sides, {a,b,c}, there are exactly three solutions for p = 120.

{20,48,52}, {24,45,51}, {30,40,50}

For which value of p ≤ 1000, is the number of solutions maximised?

The formula for the perimeter is p = a + b + c. Since we are dealing with right angled triangles, we know that a2 + b2 = c2. Knowing these, we can solve this problem by checking for successive values of a and b which could be part of a Pythagorean triple.

Solving Project Euler: Problem 038

Problem 38 is yet another dealing with pandigital numbers. Let’s look at the problem description first.

Take the number 192 and multiply it by each of 1, 2, and 3:

192 × 1 = 192
192 × 2 = 384
192 × 3 = 576

By concatenating each product we get the 1 to 9 pandigital, 192384576. We will call 192384576 the concatenated product of 192 and (1,2,3)

The same can be achieved by starting with 9 and multiplying by 1, 2, 3, 4, and 5, giving the pandigital, 918273645, which is the concatenated product of 9 and (1,2,3,4,5).

What is the largest 1 to 9 pandigital 9-digit number that can be formed as the concatenated product of an integer with (1,2, … , n) where n > 1?

To simplify the problem statement, we are looking for a number whose successive multiples when concatenated use all the digits from 1 to 9.

This problem would have been a lot tougher if we were dealing with multiples in any order. Since the multiples are in ascending order beginning with 1, we can solve it by concatenating successive multiples of a number until the concatenated string is nine digits long. The largest such concatenated string should give us our answer.

Solving Project Euler: Problem 037

Problem 37 is titled Truncatable Primes, and deals with prime numbers which when its digits are truncated also result in prime numbers.

The number 3797 has an interesting property. Being prime itself, it is possible to continuously remove digits from left to right, and remain prime at each stage: 3797, 797, 97, and 7. Similarly we can work from right to left: 3797, 379, 37, and 3.

Find the sum of the only eleven primes that are both truncatable from left to right and right to left.

NOTE: 2, 3, 5, and 7 are not considered to be truncatable primes.

To solve this problem, we will start check if each number starting from a lower bound is prime, and we will check if it is still prime when its digits are truncated successively. We could perform the truncation a couple of ways — dividing by powers of 10 and using the quotient and remainder, or by converting the number to a string and dropping digits. The code below takes the latter approach.