Project Euler Problem 040

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 $d_n$ 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}$

Solution

Straight-forward solution using Python:

from functools import reduce
 
if __name__ == '__main__':
    decimal_part = []
    i = 1
    while len(decimal_part) < 1000000:
        decimal_part.append(str(i))
        i += 1
    decimal_part = ''.join(decimal_part)
    result = reduce(lambda x, y: x * int(decimal_part[int('9' * y)]), range(1, 6), int(decimal_part[0]))
    print("The result is:", result)

The Python file is available for download here.

Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License