programming-examples/python/Misc/Python program where you take any positive integer n, if n is even, divide it by 2 to get n (by) 2. If n is odd, multiply it by 3 and add 1 to obtain 3n + 1.py

16 lines
337 B
Python
Raw Normal View History

2019-11-15 12:59:38 +01:00
def collatz_sequence(x):
num_seq = [x]
if x < 1:
return []
while x > 1:
if x % 2 == 0:
x = x / 2
else:
x = 3 * x + 1
# Added line
num_seq.append(x)
return num_seq
print(collatz_sequence(12))
print(collatz_sequence(19))