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
123456789 -> len 9
1011121314151617181920212223...9899 -> 2*90
100101102103...998999 -> 3*900
100010011002...99989999 -> 4*9000
1..9
10..2*90
2x90+1..3*900
In [1]:
import math
def nth_digit(n):
s = 0
i = 1
while s + 9*i*10**(i-1) < n:
s += 9*i*10**(i-1)
i += 1
m = str(10**(i-1) + int(math.ceil((n - s)*1.0/i)) - 1)
return int(m[(n-s-1)%i])
assert nth_digit(n=12) == 1
assert nth_digit(n=9) == 9
assert nth_digit(n=9+2*90+3*900+4*3) == 2
assert nth_digit(n=196) == 1 # 1 in 102
assert nth_digit(n=197) == 0 # 0 in 102
assert nth_digit(n=198) == 2 # 2 in 102
In [2]:
p = [nth_digit(10**d) for d in range(7)]
pr = 1
for d in p: pr *= d
print pr