Home Project Euler Teaching Projects Gallery

Problem 40: Champernowne's constant

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


Algorthim:

This is can be solved easily without coding. Let's go one by one and solve each d_n

d1 = 1. That's simple d10 = 1. Also simple

d100 does take some effort. We first traverse how many one digit we go through, which is 9. That leaves us with 91 digits to traverse to, which is 45 two digit, with one extract. Starting with 10, then we get 54 as the 45th digit starting with 10, so the next digit will be 5.

d1000, similarly, we go through 9 one digit, 90 two digit, which is total of 189 digits, leaving us with 1000 - 189 = 811 digits left. How many three digits? Well 811/3 = 270. So the 303rd digit from 100 which is 369. But thats 810 digits total, we must take into account one additional digit to make it 3.

We can keepe doing this, but you guys get the point. Well, lets do d1000000.

one digit = 9 -> 9 digits total
two digit = 90 -> 180 digits total
three digit = 900 -> 2700 digits total
four digit = 9000 -> 36000 digits total
five digit = 90000 -> 450000 digits total
Total = 488889 digits

Let's see how many digits are remaining: 1,000,000 - 488,889 = 511,111

We are on 6 digits now, so we must divide by 6. 511,111/6 = 85185 with remainder of 1. So the 85185th digit from 100,000 is 185,184 but we have a remainder of 1, so its 1!

Result:

Answer: 210
Runtime: n/a