我写了一个这样的函数:
def collatz(number):
'''
Function takes a positive integer x as input and prints the Collatz sequence starting at x
'''
while True:
if number % 2 == 0: # If number is even
number = number/2
return number
elif number == 1:
break
else:
number = 3*number+1
return number
该函数应该返回这样的数字:
collatz(10)
10
5
16
8
4
2
1
此函数适用于以下公式:
X={x/2 if x is even , if 3x+1 if x is odd
现在它只返回 1 个数字。怎么了?