"""Step 2: the extension. A census of the 3n-1 map for every start up to 1,000,000. Map: T(n) = n/2 if n is even, 3n-1 if n is odd. Unlike the Collatz map, T has three known cycles among the positive integers, with smallest members 1, 5 and 17. For every starting number up to LIMIT this script records which cycle the trajectory enters and how many steps it takes to get there, then compares the totals with the numbers stated in the paper. Run: python3 verify_2_census.py Uses only the Python standard library and no random numbers, so the output is identical on every run. Exits with status 1 if any number differs from the paper, if the fast method and the slow method disagree, or if a trajectory does something the paper says does not happen. """ import sys LIMIT = 1_000_000 CHECKPOINTS = (10, 100, 1_000, 10_000, 100_000, 1_000_000) CROSS_CHECK_LIMIT = 20_000 # every start up to here is also followed the slow way STEP_CAP = 100_000 # no trajectory below LIMIT comes anywhere near this many steps # The numbers the paper states. Table 1: how many starts 1..N enter each cycle. EXPECTED_COUNTS = { 10: (6, 4, 0), 100: (38, 31, 31), 1_000: (349, 306, 345), 10_000: (3244, 3213, 3543), 100_000: (33030, 32104, 34866), 1_000_000: (327679, 323351, 348970), } EXPECTED_CYCLE_LENGTHS = (2, 5, 18) EXPECTED_SLOWEST = (858341, 553) # (start, steps to reach a cycle) def T(n): return n // 2 if n % 2 == 0 else 3 * n - 1 def cycle_from(start): """Follow T from start until it returns to start, and return the numbers visited.""" members, x = [start], T(start) while x != start: members.append(x) x = T(x) if len(members) > 1000: raise RuntimeError(f"{start} is not on a short cycle") return members def slow_follow(n, cycle_of): """Follow n one step at a time with no shortcuts. Returns (cycle index, steps to first cycle member).""" x, count = n, 0 while x not in cycle_of: x = T(x) count += 1 if count > STEP_CAP: raise RuntimeError(f"{n} did not reach a known cycle in {STEP_CAP} steps") return cycle_of[x], count def main(): cycles = [cycle_from(1), cycle_from(5), cycle_from(17)] lengths = tuple(len(c) for c in cycles) cycle_of = {member: index for index, cycle in enumerate(cycles) for member in cycle} print("cycles (smallest member first):") for cycle in cycles: print(f" length {len(cycle):2d}: {' -> '.join(map(str, cycle))}") # which[n] = index of the cycle n enters, steps[n] = steps until n first lands on a cycle member. # Starts are handled in increasing order. A trajectory is followed only until it lands on a cycle member # or drops below its starting number, where the answer is already stored. which = bytearray(LIMIT + 1) steps = [0] * (LIMIT + 1) counts = [0, 0, 0] table = {} slowest_start, slowest_steps = 1, 0 for n in range(1, LIMIT + 1): x, count = n, 0 while x not in cycle_of and x >= n: x = T(x) count += 1 if x == n or count > STEP_CAP: # A return to n would be a fourth cycle. None is known, and the paper claims none up to LIMIT. print(f"FAIL: start {n} returned to itself or ran past {STEP_CAP} steps") return 1 if x in cycle_of: which[n], steps[n] = cycle_of[x], count else: which[n], steps[n] = which[x], count + steps[x] counts[which[n]] += 1 if steps[n] > slowest_steps: slowest_start, slowest_steps = n, steps[n] if n in CHECKPOINTS: table[n] = tuple(counts) print() print("Table 1. Starts 1..N entering each cycle (count and share)") print(f"{'N':>9} {'cycle of 1':>18} {'cycle of 5':>18} {'cycle of 17':>18}") for n in CHECKPOINTS: cells = " ".join(f"{c:>9} ({100 * c / n:5.2f}%)" for c in table[n]) print(f"{n:>9} {cells}") print() print(f"slowest start up to {LIMIT}: {slowest_start}, {slowest_steps} steps to reach a cycle") failures = [] if lengths != EXPECTED_CYCLE_LENGTHS: failures.append(f"cycle lengths {lengths}, paper says {EXPECTED_CYCLE_LENGTHS}") for n in CHECKPOINTS: if table[n] != EXPECTED_COUNTS[n]: failures.append(f"N={n}: counts {table[n]}, paper says {EXPECTED_COUNTS[n]}") if sum(table[n]) != n: failures.append(f"N={n}: counts do not add up to N") if (slowest_start, slowest_steps) != EXPECTED_SLOWEST: failures.append(f"slowest start {(slowest_start, slowest_steps)}, paper says {EXPECTED_SLOWEST}") # Control: redo the first CROSS_CHECK_LIMIT starts, and the slowest start, the slow way and compare. if slow_follow(slowest_start, cycle_of) != (which[slowest_start], steps[slowest_start]): failures.append(f"fast and slow methods disagree at the slowest start {slowest_start}") for n in range(1, CROSS_CHECK_LIMIT + 1): if slow_follow(n, cycle_of) != (which[n], steps[n]): failures.append(f"fast and slow methods disagree at start {n}") break else: print(f"cross-check: fast and slow methods agree for every start up to {CROSS_CHECK_LIMIT}") if failures: for failure in failures: print("FAIL:", failure) return 1 print("PASS") return 0 if __name__ == "__main__": sys.exit(main())