2017-06-12 19:39:10 +08:00
|
|
|
#! python3
|
|
|
|
|
"""Calculate collatz function"""
|
|
|
|
|
|
2017-06-14 16:13:51 +08:00
|
|
|
|
2017-06-12 19:39:10 +08:00
|
|
|
def collatz(x_value):
|
|
|
|
|
'''collatz calculation'''
|
|
|
|
|
if x_value % 2 == 0:
|
|
|
|
|
x_value = x_value // 2
|
2017-04-18 16:20:34 +08:00
|
|
|
else:
|
2017-06-12 19:39:10 +08:00
|
|
|
x_value = 3 * x_value + 1
|
|
|
|
|
print(x_value)
|
|
|
|
|
return x_value
|
2017-04-18 16:20:34 +08:00
|
|
|
|
2017-06-14 16:13:51 +08:00
|
|
|
|
2017-06-12 19:39:10 +08:00
|
|
|
def get_input():
|
|
|
|
|
'''get custom input'''
|
2017-04-18 16:20:34 +08:00
|
|
|
try:
|
|
|
|
|
return int(input())
|
|
|
|
|
except (ValueError, NameError):
|
|
|
|
|
print('Your should input an integer.')
|
2017-06-12 19:39:10 +08:00
|
|
|
return get_input()
|
|
|
|
|
|
|
|
|
|
N = get_input()
|
2017-04-18 16:20:34 +08:00
|
|
|
|
2017-06-12 19:39:10 +08:00
|
|
|
if N == 0:
|
2017-04-18 16:20:34 +08:00
|
|
|
print('It would enter an infinite loop.')
|
|
|
|
|
else:
|
2017-06-12 19:39:10 +08:00
|
|
|
while N != 1:
|
|
|
|
|
N = collatz(N)
|
|
|
|
|
print(N)
|