考拉茲猜想
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