希尔顿序列(Hailstone Sequence)问题(冰雹猜想)。
即:对任一正整数 n,若为偶数则除以 2,若为奇数则乘 3 再加 1,最后 n 总会变为 1。
def hailstone(n):
"""Print the hailstone sequence starting at n and return its
length.
>>> a = hailstone(10) //运行这个函数
10
5
16
8
4
2
1
>>> a //打印这个函数的返回值
7
"""
print(n);
count=1;
if(n==1):
return count;
while(n>1):
if(n%2==0):
n=n//2;
print(n);
if(n%2==1 and n!=1):
n=n*3+1;
print(n);
count+=1;
return count+1;
存下序列输出的话,加入以下三句
hailstone=[]
halistone.append(n)
return halistone