Python Implementation of Problem 40

View source code here on GitHub!

Includes

Problem Solution

Project Euler Problem 40

Problem:

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

0.12345678910(1)112131415161718192021...

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

python.src.p0040.main() int
 1"""
 2Project Euler Problem 40
 3
 4Problem:
 5
 6An irrational decimal fraction is created by concatenating the positive integers:
 7
 80.12345678910(1)112131415161718192021...
 9
10It can be seen that the 12th digit of the fractional part is 1.
11
12If dn represents the nth digit of the fractional part, find the value of the following expression.
13
14d1 × d10 × d100 × d1000 × d10000 × d100000 × d1000000
15"""
16from functools import reduce
17from itertools import count
18from math import log10
19from operator import mul
20
21
22def main() -> int:
23    digits_seen = 1
24    interests = {1, 10, 100, 1_000, 10_000, 100_000, 1_000_000}
25    queries = []
26    for x in count(1):
27        digits = int(log10(x)) + 1
28        for idx in range(digits):
29            if digits_seen + idx in interests:
30                queries.append(int(str(x)[idx]))
31                if len(queries) == len(interests):
32                    return reduce(mul, queries, 1)
33        digits_seen += digits
34    return -1  # pragma: no cover

Tags: sequence-generator, decimal-representation