-
Notifications
You must be signed in to change notification settings - Fork 0
/
SphereCollatz.py
executable file
·160 lines (131 loc) · 2.87 KB
/
SphereCollatz.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
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
#!/usr/bin/env python3
import sys
class Cache:
def __init__(self,size):
self.cache=[0]*size
# -------------
# write_cache
# -------------
def write(self,i,val):
try:
assert i > 0
if (self.cache[i-1] > 0): #then already wrote val
return
self.cache[i-1]=val
except IndexError:
pass
# -------------
# read_cache
# -------------
def read(self,i):
try:
assert i > 0
return self.cache[i-1]
except IndexError:
return 0
def size(self):
return len(self.cache)
# ------------
# collatz_read
# ------------
def collatz_read (r) :
"""
read two ints
r is a reader
return a list of the two ints, otherwise a list of zeros
"""
s = r.readline()
if s == "" :
return []
a = s.split()
return [int(v) for v in a]
# ------------
# collatz_eval
# ------------
def collatz_eval (i, j) :
"""
i is the beginning of the range, inclusive
j is the end of the range, inclusive
return the max cycle length in the range [i, j]
"""
# <your code>
assert i > 0
assert j > 0
c_len=0
result=0
k=max(i,j)
i=min(i,j)
m=k>>1
"""optimization 2
let i,j natural numbers
if i<=j and i < j/2 then
collatz_eval(i,j)=collatz_eval(j/2,j)
"""
if (i < m):
return collatz_eval(m,k)
assert i <= k
#initialize a cold cache
cache = Cache(k)
assert cache.size() == k
while( k >= i):
result=cycle_length(k,cache)
if (c_len < result) :
c_len=result
k-=1
assert c_len > 0
return c_len
# -------------
# cycle_length
# -------------
def cycle_length (n,cache) :
assert n > 0
c = 1
m = n
while n > 1 :
x = cache.read(n)
if (x > 0):
c = (c + x) - 1
assert c > 0
cache.write(m,c)
return c
if (n % 2) == 0 :
n = (n >> 1)
c+=1
else : #calculates (3n+1)/2
n = n + (n>>1) + 1
c += 2
assert c > 0
cache.write(m,c)
return c
# -------------
# collatz_print
# -------------
def collatz_print (w, i, j, v) :
"""
print three ints
w is a writer
i is the beginning of the range, inclusive
j is the end of the range, inclusive
v is the max cycle length
"""
w.write(str(i) + " " + str(j) + " " + str(v) + "\n")
# -------------
# collatz_solve
# -------------
def collatz_solve (r, w) :
"""
read, eval, print loop
r is a reader
w is a writer
"""
while True :
a = collatz_read(r)
if not a :
return
i, j = a
v = collatz_eval(i, j)
collatz_print(w, i, j, v)
# ----
# main
# ----
collatz_solve(sys.stdin, sys.stdout)