-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproblem_026_01.py
More file actions
200 lines (164 loc) · 4.83 KB
/
problem_026_01.py
File metadata and controls
200 lines (164 loc) · 4.83 KB
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
##A unit fraction contains 1 in the numerator. The decimal representation
##of the unit fractions with denominators 2 to 10 are given:
##
##1/2 = 0.5
##1/3 = 0.(3)
##1/4 = 0.25
##1/5 = 0.2
##1/6 = 0.1(6)
##1/7 = 0.(142857)
##1/8 = 0.125
##1/9 = 0.(1)
##1/10 = 0.1
##Where 0.1(6) means 0.166666..., and has a 1-digit recurring cycle.
##It can be seen that 1/7 has a 6-digit recurring cycle.
##
##Find the value of d < 1000 for which 1/d contains
##the longest recurring cycle in its decimal fraction part.
import numpy as np
import time
import functools
import math
def unitfraction(number):
return float((1/number))
def recurring(number):
#Convert to text the decimal part, after the decimal point, using
#"Decimal", 53 decimals.
from decimal import Decimal
number = Decimal(number)
decimalpart = str(number)[2:]
l0 = [c for c in decimalpart]
return l0
def checkrepetition(orderedlist):
l0 = orderedlist[:]
repetitionlist = []
for k in range(0, len(l0)-1):
# makes a second list starting in the next element.
l1 = l0[k+1:]
## print(l1)
# checks if the element(k) in L0 is repeated.
if l0[k] in l1:
# finds the position at which the element is repeated
ind1 = l1.index(l0[k])
## print(ind1)
# and modifies the list so that it starts in the repeated element.
l1 = l1[ind1:]
a = k
b = 0
seq = ""
## print("l0[k] ", l0[k], " l1 ", l1, " l1[b] ", l1[b])
while l0[a] == l1[b] and b < (len(l1)-1):
## print("seq", seq)
seq += l0[a]
a += 1
b += 1
## print("seq += l0[a]", seq)
if len(seq) > 1 and (seq[0] != seq[1]):
repetitionlist.append(seq)
value = 0
#### print("RL ", repetitionlist)
## input()
for element in repetitionlist:
element, perelement = periodicity(element)
if perelement > value:
## print("value < perelement",value, perelement)
value = perelement
## print("printing list and value ", l0, value)
return value
def periodicity(string):
s = string
k = len(s)
l = []
for n in range(0,k-1):
if n != 0 and s[0] == s[n] and s[1] == s[n+1]:
l.append(n)
## print("periodicity", s, n)
try:
number = min(l)
except:
number = 0
## print(s,number)
## input()
return s, number
def test26(number):
## print("0 ",number)
v = unitfraction(number)
## print("1 ", v)
l = recurring(v)
## print("2 ", l)
v = checkrepetition(l)
## print("3 ", v)
return number, v
def test26b(number):
r = -1
counter = 0
divnumb = 10
while r != 1:
if counter > 1000:
return number, 0
if r == 0:
return number, 0
else:
counter += 1
if r == divnumb % number:
## print(number, r, end = " ")
## print(r, divnumb, counter)
return number, counter
else:
r = divnumb % number
divnumb *= 10
## print(number, r, end = " ")
## print(r, divnumb, counter)
return number, counter
def test26c(number):
length,i = max((recur_len(i),i) for i in range(2,number))
return length, i
import itertools
def recur_len(n):
# digits for unit fraction 1/n
r = 10 # initial remainder (10/n)/10
seen = {} # remainder -> first pos
for i in itertools.count(0):
if r == 0:
return 0 # divides evenly.
elif r in seen:
return i-seen[r] # curpos - firstpos
seen[r] = i
r= 10*(r % n)
##
##send n to 1/n and return value
##send value to a function to evaluate the recurring cycle
##evaluate the presence of recurring cycle:
## for each combination
##compare the length of the recurring cycle with previous max length, replace
def euler26(number):
listofnumbers = [n for n in range(1,number)]
dictn = {}
maxdecimal = 0
for n in listofnumbers:
x, y = test26(n)
dictn[y] = x
if y > maxdecimal:
maxdecimal = y
print(maxdecimal, dictn[maxdecimal])
print(dictn)
def euler26b(number):
listofnumbers = [n for n in range(1,number)]
dictn = {}
maxdecimal = 0
for n in listofnumbers:
x, y = test26b(n)
dictn[y] = x
if y > maxdecimal:
maxdecimal = y
print(maxdecimal, dictn[maxdecimal])
## print(dictn)
def euler26c(number):
a, b = test26c(number)
print(a,b)
def main():
start = time.time()
euler26c(1000)
end = time.time()
print("\nTime elapsed\t", end -start)
main()