-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlinear_reverse_collatz.py
More file actions
59 lines (53 loc) · 1.48 KB
/
linear_reverse_collatz.py
File metadata and controls
59 lines (53 loc) · 1.48 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
def linearlyReversedCollatz(n, fullPaths=True, sequential=True):
def nextSequentialRoot(lastRoot, _):
return lastRoot + 6
def nextChaoticRoot(_, n):
return (n // 4) +5
def belongsToOddLessThan(lastRoot, n):
x = n
while x % 2 == 0:
x //= 2
return x < lastRoot
lastRoot = -3
visitedOne = False
if sequential:
nextRoot = nextSequentialRoot
else:
nextRoot = nextChaoticRoot
while True:
yield n
if n % 2 == 0:
condition = n % 8 == 0
if fullPaths:
condition = condition and belongsToOddLessThan(lastRoot, n)
if condition:
n = nextRoot(lastRoot, n)
lastRoot = n
else:
n //= 2
else:
if n != 1:
n = n * 3 + 1
else:
if not visitedOne:
visitedOne = True
n = n * 3 + 1
else:
n = nextRoot(lastRoot)
lastRoot = n
def main():
n = linearlyReversedCollatz(1)
highest = 0
while True:
#input()
x = next(n)
#if x % 2 == 1 and x % 3 == 0:
# print(x, 'root')
#else:
# print(x)
if x % 2 == 1 and x % 3 == 0:
if x > highest:
highest = x
print(highest, 'root')
if __name__ == '__main__':
main()