黄金比.再帰
Upcoming SlideShare
Loading in...5
×
 

黄金比.再帰

on

  • 1,089 views

 

Statistics

Views

Total Views
1,089
Views on SlideShare
1,089
Embed Views
0

Actions

Likes
1
Downloads
2
Comments
0

0 Embeds 0

No embeds

Accessibility

Categories

Upload Details

Uploaded via as Microsoft PowerPoint

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

    黄金比.再帰 黄金比.再帰 Presentation Transcript

    • 黄金比 . 再帰 Lispで再帰と言えば fact, fib 黄金比は見つからないので考えた 『 Scheme 手習い』読書会 #2 LT @manozo 15 AUG 2011
    • 黄金比 1:1.618033988749894848
      • A4の紙を2つに折ってA5
      • A5の紙を2つに折ってA6
      • 相似
    • 電 卓
      • (/ (+ 1 (sqrt 5)) 2)
      • 1.618034
      • これでは, つまらない
    • 連分数 じーと見ていると再帰が使えそう (+ 1 (/ (+ 1 (/ (+ 1 (/ (+ 1 (/ (+ 1 (/        1.0) ))))))))) 1.625 ;; 再帰はお尻から
    • gaucheで黄金比
      • (define gold
      • (lambda (n)
      • (cond
      • ((zero? n) 1)
      • (else
      •        (+ 1 (/.
      •            (gold (- n 1))))))))
      • (gold 100)
      • gosh> 1.61803398874989 5
      • (/ (+ 1 (sqrt 5)) 2); 1.61803398874989 4848
    • もうひとつ
      • (sqrt (+ 1
      • (sqrt (+ 1
      • (sqrt (+ 1
      • (sqrt (+ 1
      • (sqrt (+ 1
      • (sqrt (+ 1
      •             1)))))))))))) ; さて再帰でどう書く?
    • factも同様に考えると 1    1) )))) 0 1 (* 1 1) 1 2 (* 2 1) 2 6 (* 3 2) 3 24 (* 4 6) 4 120 (* 5 24) 5 値 (fact 5) n 再 帰 (zero? n) 赤字が見えると理解できる
    • おまけ 白金比, 青銅比 xyzzy で
      • ;; 白金比
      • (defun pt (n)
      • (cond
      • ((zerop n) 1.0)
      • (t (+ 2 (/ (pt (1- n)))))))
      • (pt 100)
      • 2.414214 ;; (1- (pt 100)) √2
      • ;; 青銅比
      • (defun bronze (n)
      • (cond
      • ((zerop n) 1.0)
      • (t (+ 3 (/ (bronze (1- n)))))))
      • (bronze 100)
      • 3.302776
    •  3.14159 26535 89793 23846...
      • (defun pi (n)
      • (pi1 (remove-if 'evenp (iota n))))
      • (defun pi1 (l)
      • (cond
      • ((null l) 1 )
      • (t (cond
      • ((= (car l) 1)
      • (+ 3.0 (/ (* (car l) (car l))
      • (pi1 (cdr l)))))
      • (t (+ 6.0 (/ (* (car l) (car l))
      • (pi1 (cdr l)))))))))
      • (pi 1000)
      • 3.14159 3
    •  gauche 3.14159 26535 89793 23846...
      • (define pi
      • (lambda (n)
      • (pi1 (remove even? (iota n 1)))))
      • (define pi1
      • (lambda (l)
      • (cond
      • ((null? l) 1 )
      • (else
      • (cond
      • ((= (car l) 1)
      • (+ 3 (/. (* (car l) (car l))
      • (pi1 (cdr l)))))
      • (else (+ 6 (/. (* (car l) (car l))
      • (pi1 (cdr l))))) )))))
      • (pi 1000000) ;; こんなんでもメチャメチャ早い!
      • gosh> 3.141592653589793
      ;;(iota n 1) (use srfi-1 ) gosh> ;( time (pi 180000)) ; real 0.344 ; user 0.297 ; sys 0.000 3.141592653589793
    •  gauche 2 改造 3.14159 26535 89793 23846...
      • (define pi1
      • (lambda (l) ; l:(1 3 5 7 9...) 奇数のリスト
      • (cond
      • ((null? l) 1 )
      • (else
      • (+ (if (= (car l) 1) 3 6) ;; 3 or 6
      • (/. (* (car l) (car l))
      • (pi1 (cdr l))))))))
      割り算っぽく if も式なので値を返す 3項演算子風
    • これから 再帰 を考えた 連分数はまさに再帰?
      • (+ 3 (/ (* 1 1)
      • (+ 6 (/ (* 3 3)
      • (+ 6 (/ (* 5 5)
      • (+ 6 (/ (* 7 7)
      • (+ 6 (/ (* 9 9) ;; 再帰
      • 1.0 ;; 締め
      • ))))))))))
      • gosh> 3.145 238095238095
    • まとめ
      • 連分数は再帰的?
      • 再帰はワンライナー生成器?
        • つまり出口は一つ
    • 参 考
      • xyzzy(win)でscheme-mode(gauche)使用
      • 連分数 wiki
      • http://ja.wikipedia.org/wiki/%E9%80%A3%E5%88%86%E6%95%B0
      • 黄金比 wiki
      • http://ja.wikipedia.org/wiki/%E9%BB%84%E9%87%91%E6%AF%94