Advertisement
Guest User

24-bit Collatz

a guest
Jun 8th, 2025
13
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.41 KB | None | 0 0
  1. import time
  2. import os
  3.  
  4. BIT24 = 2**24
  5. def add_arrays(a, b):
  6. """Add two 24-bit arrays."""
  7. max_len = max(len(a), len(b))
  8. result = []
  9. carry = 0
  10. for i in range(max_len):
  11. x = a[i] if i < len(a) else 0
  12. y = b[i] if i < len(b) else 0
  13. total = x + y + carry
  14. result.append(total % BIT24)
  15. carry = total // BIT24
  16. if carry:
  17. result.append(carry)
  18. return result
  19.  
  20. def multiply_array_by_3(array):
  21. """Multiply a 24-bit array by 3."""
  22. result = []
  23. carry = 0
  24. for val in array:
  25. total = val * 3 + carry
  26. result.append(total % BIT24)
  27. carry = total // BIT24
  28. if carry:
  29. result.append(carry)
  30. return result
  31.  
  32. def divide_array_by_2(array):
  33. """Divide a 24-bit array by 2."""
  34. result = []
  35. carry = 0
  36. for val in reversed(array):
  37. current = carry * BIT24 + val
  38. result.append(current // 2)
  39. carry = current % 2
  40. result.reverse()
  41. while len(result) > 1 and result[-1] == 0:
  42. result.pop()
  43. return result
  44.  
  45. def collatz_step(array):
  46. """Perform one Collatz step directly on a 24-bit array."""
  47. if array[0] % 2 == 0:
  48. return divide_array_by_2(array)
  49. else:
  50. triple = multiply_array_by_3(array)
  51. return add_arrays(triple, [1])
  52.  
  53. # === Main Run ===
  54. input_array = [6631675,357,52821084]
  55. original_input = input_array.copy()
  56.  
  57. step_count = 0
  58. timestamp = time.strftime("%Y%m%d_%H%M%S")
  59. temp_filename = f"collatz_{timestamp}_TEMP.txt"
  60. # Track highest array seen
  61. highest_array = input_array.copy()
  62.  
  63. # Start timer
  64. start_time = time.time()
  65.  
  66. with open(temp_filename, "w") as f:
  67. f.write(f"S{step_count}: [{input_array[0]}] [{input_array[-1]}] ({len(input_array)})\n")
  68. while input_array != [1]:
  69. input_array = collatz_step(input_array)
  70. step_count += 1
  71. f.write(f"S{step_count}: [{input_array[0]}] [{input_array[-1]}] ({len(input_array)})\n")
  72.  
  73. # Check if current array is the "largest" seen so far
  74. is_new_high = False
  75. if len(input_array) > len(highest_array):
  76. is_new_high = True
  77. elif len(input_array) == len(highest_array):
  78. if input_array[-1] > highest_array[-1]:
  79. is_new_high = True
  80. if is_new_high:
  81. highest_array = input_array.copy()
  82.  
  83. # Stop timer
  84. end_time = time.time()
  85. runtime_sec = end_time - start_time
  86.  
  87. # Rename step log file
  88. final_filename = f"collatz_{timestamp}_steps_{step_count}.txt"
  89. os.rename(temp_filename, final_filename)
  90.  
  91. # Prepare short version of original input
  92. short_version = f"[{original_input[0]}] ({len(original_input)}) [{original_input[-1]}]"
  93. # Write summary file
  94. summary_filename = f"collatz_{timestamp}_summary.txt"
  95. with open(summary_filename, "w") as f:
  96. f.write(f"Collatz Summary\n")
  97. f.write(f"===============\n")
  98. f.write(f"Input array: {original_input}\n")
  99. f.write(f"Input array (short version): {short_version}\n")
  100. f.write(f"Number of steps: {step_count}\n")
  101. f.write(f"Runtime (seconds): {runtime_sec:.6f}\n")
  102. f.write(f"Highest value array reached: {highest_array}\n")
  103. f.write(f"Highest array length: {len(highest_array)}\n")
  104. f.write(f"Highest array right-most cell value: {highest_array[-1]}\n")
  105.  
  106. # Final print
  107. print(f"Finished in {step_count} steps.")
  108. print(f"Steps saved to: {final_filename}")
  109. print(f"Summary saved to: {summary_filename}")
  110.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement