L4K ︰ Python Turtle《十上》

小海龜移居『洛水』途中,聽聞了一則古老之傳說

300px-Tower_of_Hanoi

 

一八八三年法國數學家 Édouard Lucas 講了一個傳說

印度梵天寺有『』三根柱子,柱上串有側見從上往下是由小到大的六十四金盤子,寺院裡的僧侶必須按照一 個古老的預言,預言裡面有宣示的規矩,將成柱上的這些金盤子,大的不能放在小的之上一次又只能動一柱之最上的一個金盤,或可藉或不藉它柱,移往柱,一旦依法奉行完成,世界就會空亡

於是其後有人問到依照預言『這個世界多久就會毀滅』?作者並不知道梵天寺之『浮屠』── ──的傳說是否真實?然而如果想要回答那個問題,得要先談談『遞迴』Recursion 一事,就讓我們先從『數學歸納法』說起吧!

事實上數學歸納法並不是個歸納法,而是個演繹法,它有多種版本變化,這裡講的是普通的一種

比方說有若干事件 Event ,讓我們依序排列成
E_1E_2,…,E_k,…,E_nE_{n+1}
已知
第一點E_1  事件是真的
第二點,假使 E_n 事件是真的,可以邏輯導出 E_{n+1} 事件也是真的
,那麼結論所有的這些事件都是真的

在數學裡,數學歸納法很常見,不只經常用來『證明』問題,還可以用來『定義遞迴性的東西。舉例說『階乘』factorial 可以這樣
定義
0!=1
1!=1

n!= n * (n-1)!

。這種方式的好處是什麼呢?它從已知的推向未知的,由已會算的導往還不會算的,彰顯遞迴結構的『核心』,又很簡約所以容易掌握!現在就讓我們分析一下那個傳說的預言── 別稱『河內塔』──的問題

第一條、假如『成』柱上只有一個金盤子,直接將它移至『壞』柱就完成了

第二條、如果『成』柱上有 n + 1 個金盤子,先將上面 n 個移至『住』柱,再將最後一個移至『壞』柱

第三條、最後將『住』柱上的 n 個金盤子,移至『壞』柱

,這三條思路,使我們知道從怎麽處理『』個金盤子,到處理『』個金盤子,然後能處理『』個金盤子,……,以致最終能處理『任意』個金盤子。所以說如果處理 n 個金盤子需要 S_n  步次,那麼就會有這樣子的關係式

S_{n+1} = S_n + 1 + S_n

,如果我們將這個式子改寫成

S_{n+1}  + 1 = 2 * (  S_n + 1  )

,它變成一個以 2數的等比級數,所以得到 S_n = 2^n – 1。

據《摩訶僧祇律十七》︰則謂

『二 十念名為一瞬頃,二十瞬名為一彈指,二十彈指名為一羅豫,二十羅豫名為一須臾,日極長時,有十八須臾,夜極短時,有十二須臾,夜極長時,有十八須臾,日極 短時,有十二須臾。』此即一晝夜為三十須臾,一須臾二十分為一羅豫,一羅豫二十分為一彈指,一彈指二十分為一瞬,一瞬二十分即為一念之說也。又《華嚴探玄記十八》謂剎那茲云念頃,一彈指頃有六十剎那。

意思是說一天二十四小時三十個『須臾』=六百個『羅豫』=一萬兩千個『彈指』=七十二萬個『剎那』,所以一剎那是 86400 /720000 = 0.12 秒。那也就是說 2^{64}  – 1 的數字之大,即使『念頭一轉就能移動一個金盤子,『這個世界要毀滅』還得要超過七百億年的時間才有可能!!

─── 摘自《啃一塊唄 K TCPIP!!下

 

燃起對『遞迴』的好奇心︰

Recursion (computer science)

 Recursion in computer science is a method where the solution to a problem depends on solutions to smaller instances of the same problem (as opposed to iteration).[1] The approach can be applied to many types of problems, and recursion is one of the central ideas of computer science.[2]

“The power of recursion evidently lies in the possibility of defining an infinite set of objects by a finite statement. In the same manner, an infinite number of computations can be described by a finite recursive program, even if this program contains no explicit repetitions.”[3]

Most computer programming languages support recursion by allowing a function to call itself within the program text. Some functional programming languages do not define any looping constructs but rely solely on recursion to repeatedly call code. Computability theory proves that these recursive-only languages are Turing complete; they are as computationally powerful as Turing complete imperative languages, meaning they can solve the same kinds of problems as imperative languages even without iterative control structures such as “while” and “for”.

Tree created using the Logo programming language and relying heavily on recursion

 

因此苦練『階乘』之術︰

pi@raspberrypi:~ $ python3
Python 3.4.2 (default, Oct 19 2014, 13:31:11) 
[GCC 4.9.1] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import turtle, math
>>> turtle.setup(width=1024, height=768)
>>> turtle.shape('turtle')
>>> turtle.penup()
>>> turtle.backward(300)
>>> turtle.pendown()
>>> 
>>> def 小方塊():
...     turtle.left(45)
...     for n in range(4):
...         turtle.forward(3)
...         turtle.right(90)
...     turtle.right(45)
... 
>>> 
>>> def 階乘(n):
...     if n < 2:
...         小方塊()
...     else:
...         turtle.left(10*n)
...         for m in range(n):
...             h = turtle.heading()
...             turtle.forward(100)
...             階乘(n-1)
...             turtle.seth(h)
...             turtle.backward(100)
...             turtle.right(20)
... 
>>> 
>>> 階乘(3)
>>> 
>>> turtle.reset();turtle.penup();turtle.backward(300);turtle.pendown();階乘(4)
>>> 

 

【3!】

 

【4!】

 

發心願出天地間也!!??