Python Day 4: Collatz Conjecture
原来总有学生问我,微积分有什么用啊, 我说如果微积分学好了,也许抽象代数和数论就能学好,那最后就能像Andrew Wiles 一样上 人物 年度杂志的封面了. (Andrew Wiles 证明了Fermat's Last Theorem,费玛大定理).
[caption id="attachment_1466" align="alignnone" width="300"] zractapopulous / Pixabay[/caption]
数论里还有很多很容易了理解,但还没有证明的猜想,像 the Collatz 猜想.
Collatz Conjecture:
- Take any natural number, n.
- If n is even, divide it by 2.
- Otherwise, n is odd. Multiply it by 3 and add 1
- Repeat indefinitely, the number will converges to 1 for finitely many steps.
Mathematicians could not find a counterexample, however, there is no formal proof for Collatz Conjecture. Therefore the problem still remains unsolved.
I wrote short python code to test the algorithm, the numbers I checked did converge to 1.
But, as my maths professor always says:
"For example" is NOT a proof!
举例不是证明
def collatz_conjecture(x):
lists= [x]
if x<1 :
return []
while x > 1:
if x% 2==0:
x=x/2
else:
x=x*3+1
lists.append(x)
return(lists)
collatz_conjecture(6)
collatz_conjecture(93)
collatz_conjecture(180)
Output:
[6, 3.0, 10.0, 5.0, 16.0, 8.0, 4.0, 2.0, 1.0]
[93,280,140.0,70.0,35.0,106.0,53.0,160.0,80.0,40.0,20.0,10.0,5.0,16.0,8.0,4.0,2.0,1.0]
[180,90.0,45.0, 136.0,68.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,1.0]
Note: the picture is from https://xkcd.com/710/
**Happy Studying! **🥨
网友评论