Project Euler 40
Project Euler 40
题目
Champernowne’s constant
An irrational decimal fraction is created by concatenating the positive integers:
It can be seen that the $12$th digit of the fractional part is $1$.
If $d_n$ represents the $n$th digit of the fractional part, find the value of the following expression.
解决方案
每一个$d_i$都是一个独立的问题,可以先计算后再相乘。
可以发现,$1\sim 9,10\sim 99,100\sim 999,\dots,10^{n-1}\sim 10^n-1,\dots$,每一块都里面的数有相同的长度,而且,每一块的长度增长都是为$1$。
因此,计算每一个独立的$d_i$时,先找到第$i$位属于那一块,然后再判断第$i$位属于哪一个数,之后直接相乘即可。
代码
1 | from itertools import count |