Problem 40 Champernowne's constant

2016-10-21 12:45:00   技术   projecteulor python

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


钱珀瑙恩常数

将所有正整数连接起来构造的一个十进制无理数如下所示:

0.123456789101112131415161718192021… 可以看出小数点后第12位数字是1。

如果dn表示上述无理数小数点后的第n位数字,求下式的值:

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

import math

table = []
result = 1
for i in range(1, 10):
    table.append(i * 9 * 10 ** (i - 1))


def process(number):
    point = 1
    while number > sum(table[:point]):
        point += 1
    index = number - sum(table[:point - 1])
    if point == 1:
        return number
    else:
        return eval(str(10**(point - 1) + index / point)[index % point - 1])


for i in range(1, 8):
    result *= process(10 ** (i - 1))

print result

有一种标准的解决问题的编程的感觉,调试了很久

评论已关闭。
评论共