Created
July 23, 2025 01:56
-
-
Save igorvanloo/085a1a716147ea4f7962d57177c5a6f8 to your computer and use it in GitHub Desktop.
p209
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def find_next(curr): | |
| #Helper function to find cycles | |
| t = bin(curr)[2:][::-1] | |
| b = [int(x) for x in t + '0'*(6 - len(t))] | |
| nb = b[1:] + [b[0] ^ (b[1] & b[2])] | |
| snb = ''.join([str(x) for x in nb]) | |
| curr = int(snb[::-1], 2) | |
| return curr | |
| def lucas(n): | |
| #Finds n-th Lucas number using my Fibonacci number generator | |
| f2, f1, f0 = 1, 1, 0 | |
| for bit in bin(n)[3:]: | |
| v = f1*f1 | |
| f2, f1, f0 = f2 * f2 + v, (f2 + f0) * f1, v + f0 * f0 | |
| if bit == '1': | |
| f2, f1, f0 = f2 + f1, f2, f1 | |
| return f2 + f0 | |
| def compute(): | |
| pointer = [None] * 64 | |
| total = 1 | |
| while None in pointer: | |
| for start, x in enumerate(pointer): | |
| if x == None: | |
| break | |
| curr = find_next(start) | |
| pointer[start] = curr | |
| cycle = (start, curr) | |
| while curr != start: | |
| t = find_next(curr) | |
| cycle += (t, ) | |
| pointer[curr] = t | |
| curr = t | |
| total *= lucas(len(cycle) - 1) | |
| return total |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment