• 对于偶数，除以2
• 对于奇数，乘以3再加上1

生成Collatz序列


#this is the generator of Collatz Sequece
#we use the programe to generate it
#the Collatz Sequece is the series that has the following character:
#if the number is even , divide it with 2
#if the number it odd, multilply it with 3 and add 1 to get the final result
#if we get 1, exit the programe
#waventropy

numStr=raw_input("Enter a postive number:")
num=int(numStr)
count=0

print "Sequece is:",
#the ending , is to set Python put the output of print in oneline

while num > 1:
if num%2:
num=num*3+1
else:
num=num/2
print num,",",

count+=1

else:
print
print "Sequece is",count,"number long"