-
Notifications
You must be signed in to change notification settings - Fork 0
/
Champernowne.py
110 lines (90 loc) · 1.97 KB
/
Champernowne.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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
import math
import sys
import DoubleBase
import Truncatable
lengthCount = [0, 9, 100, 1000, 10000, 100000, 1000000]
def getNumDigits(num):
return int(math.log10(num))+1;
def getDigits(digit):
inputDigit = digit
digit -= 1
count = 1
magnitude = 0
finished = False
if(not finished):
if(digit - 9 > 0):
digit -= 9
count += 9
else:
finished = True
magnitude = 1
if(not finished):
if(digit - 90*2 > 0):
digit -= 90*2
count += 90
else:
finished = True
magnitude = 2
if(not finished):
if(digit - 900*3 > 0):
digit -= 900*3
count += 900
else:
finished = True
magnitude = 3
if(not finished):
if(digit - 9000*4 > 0):
digit -= 9000*4
count += 9000
else:
finished = True
magnitude = 4
if(not finished):
if(digit - 90000*5 > 0):
digit -= 90000*5
count += 90000
else:
finished = True
magnitude = 5
if(not finished):
if(digit - 900000*6 > 0):
digit -= 900000*6
count += 900000
else:
finished = True
magnitude = 6
assert finished
count += int(digit/magnitude)
subDigit = digit%magnitude
print "input digit is", inputDigit
print "digit is",digit
print "magnitude is",magnitude
print "count is",count
print "subDigit is", subDigit
countOrder = getNumDigits(count)
print "countOrder is", countOrder
returnValue = DoubleBase.getIndex10(count, countOrder -subDigit-1)
print "return value is",returnValue
return returnValue
def main():
expr = 1;
for exp in range(7):
digit = int(math.pow(10,exp))
expr *= getDigits(digit)
print expr
if __name__ == "__main__":
if(len(sys.argv) > 1 and sys.argv[1] == "test"):
assert True
assert getNumDigits(10) == 2
assert getNumDigits(5) == 1
assert getNumDigits(2345) == 4
assert getNumDigits(1) == 1
assert getDigits(4) == 4
assert getDigits(14) == 1
assert getDigits(15) == 2
assert getDigits(16) == 1
assert getDigits(17) == 3
assert getDigits(18) == 1
assert getDigits(19) == 4
else:
main()