here2share

# collatz_conjecture.py

Dec 27th, 2022 (edited)
1,189
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.32 KB | None | 0 0
  1. # collatz_conjecture.py
  2.  
  3. def collatz_conjecture(n):
  4.     result = [n]
  5.     def cc(n):
  6.         if n == 1:
  7.             return
  8.         elif n % 2 == 0:
  9.             v = n // 2
  10.             result.append(v)
  11.             cc(v)
  12.         else:
  13.             v = 3 * n + 1
  14.             result.append(v)
  15.             cc(v)
  16.     cc(n)
  17.     print(result)
  18.  
  19. collatz_conjecture(10)
  20.  
  21. collatz_conjecture(15)
  22.  
  23. collatz_conjecture(99)
Advertisement
Add Comment
Please, Sign In to add comment