Collatz 猜想和 Python

Python Day 4: Collatz Conjecture

原來總有學(xué)生問我,微積分有什么用啊, 我說如果微積分學(xué)好了,也許抽象代數(shù)和數(shù)論就能學(xué)好,那最后就能像Andrew Wiles 一樣上 人物 年度雜志的封面了. (Andrew Wiles 證明了Fermat's Last Theorem,費(fèi)瑪大定理).

[caption id="attachment_1466" align="alignnone" width="300"]
z

ractapopulous / Pixabay[/caption]

數(shù)論里還有很多很容易了理解,但還沒有證明的猜想,像 the Collatz 猜想.

Collatz Conjecture:

  1. Take any natural number, n.
  2. If n is even, divide it by 2.
  3. Otherwise, n is odd. Multiply it by 3 and add 1
  4. Repeat indefinitely, the number will converges to 1 for finitely many steps.
image

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! **??

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容