You are viewing a single comment's thread. Return to all comments →
Here is the Python version of the Java code below. It works in PyPy.
def num_state(num): num_state = 0 while num != 1: if num % 2 == 0: num //= 2 else: num = 3 * num + 1 num_state += 1 return num_state # Read all input t = int(input()) arr_input = [int(input()) for _ in range(t)] max_val = max(arr_input) # Precompute answers ans = [0] * (max_val + 1) max_state = 1 steps = 1 for j in range(1, max_val + 1): state = num_state(j) if state >= max_state: max_state = state steps = j ans[j] = steps # Output results for val in arr_input: print(ans[val])
Seems like cookies are disabled on this browser, please enable them to open this website
Project Euler #14: Longest Collatz sequence
You are viewing a single comment's thread. Return to all comments →
Here is the Python version of the Java code below. It works in PyPy.