# Champernowne's constant (Euler Problem 40)¶

Back to overview.

https://projecteuler.net/problem=40

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.

$d_{1} × d_{10} × d_{100} × d_{1000} × d_{10000} × d_{100000} × d_{1000000}$

I do not see why we cannot hold $8 bytes \times 10^{6} = 8 MB$ in memory.

In [1]:

    def get_irrational_fraction(digits_max):
        s = []
        n = 1
        digits = 0
        while digits < digits_max:
            s.append(str(n))
            digits += len(str(n))
            n += 1
        return "".join(s)

    assert(get_irrational_fraction(20)[12] == "1")

    f = get_irrational_fraction(1000001)

Okay, we got the fractional. No we get the digits at the different positions and calculate the product. For that purpose we reuse our poduct function from problem 8.

In [2]:

    def product(xs):
        from operator import mul
        from functools import reduce
        return reduce(mul, xs, 1)

In [3]:

    s = product(map(int, [f[10**i - 1] for i in range(7)]))
    print(s)
    assert(s == 210)

    210