-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path040.py
executable file
·51 lines (42 loc) · 1005 Bytes
/
040.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#!/usr/bin/python
# -*- coding: utf-8 -*-
# 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
def slow_nth_digit(n):
i = 1
count = 0
while True:
for l in str(i):
count += 1
if count == n:
return int(l)
i += 1
def fast_nth_digit(n):
if n < 10:
return n
i = 1
m = 1 # because we count starting from one
while True:
skip = i * (10**i - 10**(i-1)) + m
if skip > n:
i = i-1
skip = m
q = n - skip
a = q // (i+1)
b = q % (i+1)
r = (10**i + a)//(10**(i-b))
k = r - (r//10)*10
return k
m = skip
i += 1
def main():
result = 1
for i in xrange(0,7):
result *= fast_nth_digit(10**i)
print "Result:", result
if __name__ == '__main__':
main()