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.

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

In [1]:
def digits(n):
    return [int(c) for c in str(n)]

def fractional_digits():
    n = 1
    while True:
        for i in digits(n):
            yield i
        n += 1

def search(targets):
    result = {}
    n = 0
    for d in fractional_digits():
        n += 1
        if n in targets:
            result[n] = d
            if len(result) == len(targets):
                return result

def solve():
    digit_at = search({1,10,100,1000,10000,100000,1000000})
    ans = 1
    for pos in digit_at:
        ans *= digit_at[pos]
    return ans

In [2]:
import time
t0 = time.time()
ans = solve()
t1 = time.time()
print('Answer:', ans)
print('Elapsed:', t1-t0)


Answer: 210
Elapsed: 0.8970928192138672

In [ ]: