Advertisement
Guest User

Untitled

a guest
Sep 17th, 2019
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.60 KB | None | 0 0
  1. from time import time
  2.  
  3. N = 1_000_000
  4. seq_lens = [0] * N # make an array of 0's of size N
  5. seq_lens[1] = 1
  6.  
  7. start_time = time()
  8.  
  9. max_n = 1
  10. for n in range(2, N):
  11.     l = 0
  12.     x = n
  13.     while x >= N or seq_lens[x] == 0: # either we're outside the range of the array or we're at unvisited numbers
  14.         if x % 2 == 0:
  15.             x = x // 2
  16.         else:
  17.             x = 3 * x + 1
  18.         l += 1
  19.  
  20.     # "distance" n to 1 is the "distance" of n to x plus the "distance" of x to 1.
  21.     seq_lens[n] = seq_lens[x] + l
  22.    
  23.     if seq_lens[n] > seq_lens[max_n]:
  24.         max_n = n
  25.  
  26. end_time = time()
  27.  
  28. print(max_n)
  29. print("took", end_time - start_time, "seconds")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement