hailstone Python_alogorithm
Post:2018-07-07 22:43:50
Visit:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
|
import os def hailstone(n): hai_list = [] while n > 1: hai_list.append(n) if n%2: n = 3*n + 1 else: n = n//2 else: hai_list.append(1) return hai_list def print_result(r): length=len(r) print('list:{list}\nlength:{length}'.format(list=r,length=length))
if __name__ == "__main__": print_result(hailstone(7)) print('*'*80) print_result(hailstone(27))
|
感谢鼓励