考拉兹猜想
https://www.sohu.com/a/157051762_224832
对于自然数N
f(N) = N / 2 (N为偶数)
= 3 * N + 1 (N为奇数时)
不断的进行f操作,是否最终都会收敛到1?
目前还没有数学证明。
从程序计算的角度看,是个很清晰的结构,由于Python的number的范围较大,所以使用Python去写程序是比较适宜的。
这里练习下Python的循环以及输入参数的使用。
#!/usr/bin/env python
#coding: utf-8
import sys
print("hello")
count = 0
num = 100
num = int(sys.argv[1])
while num != 1:
print(num)
count += 1
if(num % 2 == 0):
num = num / 2
else:
num = num * 3 + 1
print(count)
测试
F:\Learn\2020>python test.py 5
hello
5
16
8.0
4.0
2.0
5
F:\Learn\2020>python test.py 6
hello
6
3.0
10.0
5.0
16.0
8.0
4.0
2.0
8
F:\Learn\2020>python test.py 51
hello
51
154
77.0
232.0
116.0
58.0
29.0
88.0
44.0
22.0
11.0
34.0
17.0
52.0
26.0
13.0
40.0
20.0
10.0
5.0
16.0
8.0
4.0
2.0
24