-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy patheuler026.py
78 lines (67 loc) · 1.75 KB
/
euler026.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
"""
"""
import random
import re
import math
maxrecurr = [-1] * (10000 + 1)
lengthofrepetend = [-1] * (10000 + 1)
lengthofrepetend[1] = 0
primes = {2, 3, 5, 7, 11}
def isPrime(n):
i = 2
while i <= math.sqrt(n):
if n % i == 0 and n != i:
# lengthofrepetend[n] = -2
return False
i += 1
return True
def getPeriodLength(n):
if lengthofrepetend[n] == -1:
npr = n
while npr % 2 == 0:
npr /= 2
while npr % 5 == 0:
npr /= 5
if npr != n:
lengthofrepetend[n] = getPeriodLength(npr)
else:
notfound = True
# if (10**(n - 1) - 1) % n == 0:
# lengthofrepetend[n] = n - 1
# notfound = False
mpow = 1
while notfound:
if (10**mpow - 1) % n == 0:
lengthofrepetend[n] = mpow
notfound = False
mpow += 1
return lengthofrepetend[n]
def main():
# t = int(raw_input().strip())
# t = random.randint(1, 1000)
t = 1
k_max = 0
i_max = 1
i = 3
while t:
# n = long(raw_input().strip())
# n = long(random.randint(4, 10000))
n = 5000
if maxrecurr[n] == -1:
while i <= n - 1:
k = getPeriodLength(i)
if k > k_max:
k_max = k
i_max = max(i, i_max)
maxrecurr[i + 1] = i_max
i += 1
# print maxrecurr[4:n + 1]
print lengthofrepetend[n]
print maxrecurr[n]
t -= 1
return 0
if __name__ == '__main__':
# print "This program is being run by itself"
main()
else:
print 'I am being imported from another module'