Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import time
- import os
- BIT24 = 2**24
- def add_arrays(a, b):
- """Add two 24-bit arrays."""
- max_len = max(len(a), len(b))
- result = []
- carry = 0
- for i in range(max_len):
- x = a[i] if i < len(a) else 0
- y = b[i] if i < len(b) else 0
- total = x + y + carry
- result.append(total % BIT24)
- carry = total // BIT24
- if carry:
- result.append(carry)
- return result
- def multiply_array_by_3(array):
- """Multiply a 24-bit array by 3."""
- result = []
- carry = 0
- for val in array:
- total = val * 3 + carry
- result.append(total % BIT24)
- carry = total // BIT24
- if carry:
- result.append(carry)
- return result
- def divide_array_by_2(array):
- """Divide a 24-bit array by 2."""
- result = []
- carry = 0
- for val in reversed(array):
- current = carry * BIT24 + val
- result.append(current // 2)
- carry = current % 2
- result.reverse()
- while len(result) > 1 and result[-1] == 0:
- result.pop()
- return result
- def collatz_step(array):
- """Perform one Collatz step directly on a 24-bit array."""
- if array[0] % 2 == 0:
- return divide_array_by_2(array)
- else:
- triple = multiply_array_by_3(array)
- return add_arrays(triple, [1])
- # === Main Run ===
- input_array = [6631675,357,52821084]
- original_input = input_array.copy()
- step_count = 0
- timestamp = time.strftime("%Y%m%d_%H%M%S")
- temp_filename = f"collatz_{timestamp}_TEMP.txt"
- # Track highest array seen
- highest_array = input_array.copy()
- # Start timer
- start_time = time.time()
- with open(temp_filename, "w") as f:
- f.write(f"S{step_count}: [{input_array[0]}] [{input_array[-1]}] ({len(input_array)})\n")
- while input_array != [1]:
- input_array = collatz_step(input_array)
- step_count += 1
- f.write(f"S{step_count}: [{input_array[0]}] [{input_array[-1]}] ({len(input_array)})\n")
- # Check if current array is the "largest" seen so far
- is_new_high = False
- if len(input_array) > len(highest_array):
- is_new_high = True
- elif len(input_array) == len(highest_array):
- if input_array[-1] > highest_array[-1]:
- is_new_high = True
- if is_new_high:
- highest_array = input_array.copy()
- # Stop timer
- end_time = time.time()
- runtime_sec = end_time - start_time
- # Rename step log file
- final_filename = f"collatz_{timestamp}_steps_{step_count}.txt"
- os.rename(temp_filename, final_filename)
- # Prepare short version of original input
- short_version = f"[{original_input[0]}] ({len(original_input)}) [{original_input[-1]}]"
- # Write summary file
- summary_filename = f"collatz_{timestamp}_summary.txt"
- with open(summary_filename, "w") as f:
- f.write(f"Collatz Summary\n")
- f.write(f"===============\n")
- f.write(f"Input array: {original_input}\n")
- f.write(f"Input array (short version): {short_version}\n")
- f.write(f"Number of steps: {step_count}\n")
- f.write(f"Runtime (seconds): {runtime_sec:.6f}\n")
- f.write(f"Highest value array reached: {highest_array}\n")
- f.write(f"Highest array length: {len(highest_array)}\n")
- f.write(f"Highest array right-most cell value: {highest_array[-1]}\n")
- # Final print
- print(f"Finished in {step_count} steps.")
- print(f"Steps saved to: {final_filename}")
- print(f"Summary saved to: {summary_filename}")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement