SICPの練習問題 1.12をPythonでやってみた
(schemeの解答はこちら:http://oss.timedia.co.jp/index.cgi/kahua-web/show/SICP/Answer%20Book)
Exercise 1.12. The following pattern of numbers is called Pascal's triangle
(練習問題 1.12 パスカルの三角形と呼ばれる数の次にくるパターン)
1 1 2 1 1 3 3 1 1 4 6 4 1 ...
The numbers at the edge of the triangle are all 1, and each number inside the triangle is the sum of the two numbers above it. Write a procedure that computes elements of Pascal's triangle by means of a recursive process.
(端にくる数字はすべて1、そして三角形の内側にくる各数字はその上の2つの数字の合計。パスカルの三角形の計算要素を再帰プロセス用いて手順を書け。)
# r == 縦列(0~), c == 横列(0~) def pascal_t(r, c): if c == 0: return 1 elif r == c: return 1 else: return pascal_t(r - 1, c - 1) + pascal_t(r - 1, c) print pascal_t(4,2) # 6