SICP#3ã¾ã¨ããvol.2
次ã«é¤æ°ã®æ¢ç´¢
(define (smallest-divisor n) (find-divisor n 2 )) (define (find-divisor n test-divisor) (cond ((> (square test-divisor) n) n) (( divides? test-divisor n) test-divisor) ( else (find-divisor n (+ test-divisor 1))))) (define (devides? a b) (= (remainder b a) 0)) (define (prime? n) (= n (smallest-divisor n )))
ã³ã³ã§æã£ããã§ããã©ã¾ã
((> (square test-divisor) n) n)
ãã®ã³ã¼ãããªãã¦ãåé¡ãªããããªæ°ããã¾ãã
ãã®ã³ã¼ããèªãã¨
ãsmallest-divisorãnã¨å®ç¾©ãã ãfind-divisor nã¯2ã¨ãã ãfind-divisor nã¯test-divisorã¨å®ç¾©ãã ãtest-divisorã®äºä¹ãnãã大ãããnã®å ´å ãtest-divisorãnã§å²ã£ãæã«test-divisorã§å²ãåãã?å ´å ãâfind-divisor n ããã®ä»ã¯test-divisorã«1ã足ãã¦find-divisor nãå帰 ãaãbã§å²ãäºãåºæ¥ã?ã¨å®ç¾©ãã ãa/bã®å°ä½=0 ã nãç´ æ°?ã¨å®ç¾©ãã smallest-divisor n = n
ãããªæãã§ããã
(define (smallest-divisor n) (find-divisor n 2 ))
ç´ æ°ã¯1ã¯å«ã¾ããªãã®ã§2ããã¹ã¿ã¼ãããã®ã§ã³ã³ã§ã¯
find-divisor nã2ã¨å®ç¾©ãã¦ã¾ã
(define (devides? a b) (= (remainder b a) 0)) (define (prime? n) (= n (smallest-divisor n )))
ã³ã³ã¯ç´ æ°ã®ç¹æ§ã«ã¤ãã¦å®ç¾©ãã¦ãã ããªã®ã§ç解åºæ¥ã¾ã
(define (find-divisor n test-divisor) (cond ((> (square test-divisor) n) n) (( divides? test-divisor n) test-divisor) ( else (find-divisor n (+ test-divisor 1)))))
ã³ã³ãç解åºæ¥ããããªåºæ¥ãªããããªããã
ããã§å®éã«æ°åã代å
¥ãã¦èãã¦ããããã¨æãã¾ã
ã³ã¼ã1
(define (smallest-divisor n) (find-divisor n 2 )) (define (find-divisor n test-divisor) (cond ((> (square test-divisor) n) n) (( divides? test-divisor n) test-divisor) ( else (find-divisor n (+ test-divisor 1)))))
ã³ã¼ã2
(define (smallest-divisor n) (find-divisor n 2 )) (define (find-divisor n test-divisor) (cond (( divides? test-divisor n) test-divisor) ( else (find-divisor n (+ test-divisor 1)))))
ã³ã¼ã1ãã³ã¼ã2ã«(smallest-divisor 13)ã代å
¥ãããã¨æãã¾ã
ã¾ãã³ã¼ã1ãã
test-divisorã2ã®æ ( else (find-divisor n (+ test-divisor 1)))ã¨ãªã test-divisorã3ã®æ ( else (find-divisor n (+ test-divisor 1)))ã¨ãªã ((> (square test-divisor) n) n)ã¨ãªã 4ãäºä¹ãã16ã®æ¹ã13ãã大ãããªãã®ã§smallest-divisor 13
ã§ã¯æ¬¡ã«ã³ã¼ã2
(( divides? test-divisor n) test-divisor)㧠test-divisorã2ã®æ13ã2ã§å²ãåããªãã®ã§ ( else (find-divisor n (+ test-divisor 1))) test-divisorã3ã®æ13ã3ã§å²ãåããªãã®ã§ ( else (find-divisor n (+ test-divisor 1))) test-divisorã4ã®æ13ã4ã§å²ãåããªãã®ã§ ( else (find-divisor n (+ test-divisor 1))) test-divisorã5ã®æ13ã5ã§å²ãåããªãã®ã§ ( else (find-divisor n (+ test-divisor 1))) test-divisorã6ã®æ13ã6ã§å²ãåããªãã®ã§ ( else (find-divisor n (+ test-divisor 1))) test-divisorã7ã®æ13ã7ã§å²ãåããªãã®ã§ ( else (find-divisor n (+ test-divisor 1))) test-divisorã8ã®æ13ã8ã§å²ãåããªãã®ã§ ( eles (find-divisor n (+ test-divisor 1))) test-divisorã9ã®æ13ã9ã§å²ãåããªãã®ã§ ( else (find-divisor n (+ test-divisor 1))) test-divisorã10ã®æ13ã10ã§å²ãåããªãã®ã§ ( else (find-divisor n (+ test-divisor 1))) test-divisorã11ã®æ13ã11ã§å²ãåããªãã®ã§ ( else (find-divisor n (+ test-divisor 1))) test-divisorã12ã®æ13ã12ã§å²ãåããªãã®ã§ ( else (find-divisor n (+ test-divisor 1))) test-divisorã13ã®æ13ã13ã§å²ãåããã®ã§ smallest-divisor 13
å¦ççµæã¯åãã§ããã©å¦çã®å
容ãå
¨ç¶éãã¾ãã
ããã次ã¯ã³ã¼ã1ãã³ã¼ã2ã«(smallest-divisor 35)ã代å
¥ãããã¨æãã¾ã
ã¾ãã³ã¼ã1ãã
test-divisorã2ã®æ ( else (find-divisor n (+ test-divisor 1)))ã¨ãªã test-divisorã3ã®æ ( else (find-divisor n (+ test-divisor 1)))ã¨ãªã test-divisorã4ã®æ test-divisorã4ã®æ ( else (find-divisor n (+ test-divisor 1)))ã¨ãªã test-divisorã5ã®æ ((> (square test-divisor) n) n)ã«ã¯å½ã¦ã¯ã¾ããªããã© (( divides? test-divisor n) test-divisor)ã«ã¯å½ã¦ã¯ã¾ãã¾ã 35ã¯5ã§å²ãåãã¾ã ãªã®ã§smallest-divisor 5
ã§ã¯æ¬¡ã«ã³ã¼ã2
(( divides? test-divisor n) test-divisor)㧠test-divisorã2ã®æ35ã2ã§å²ãåããªãã®ã§ ( else (find-divisor n (+ test-divisor 1))) test-divisorã3ã®æ35ã3ã§å²ãåããªãã®ã§ ( else (find-divisor n (+ test-divisor 1))) test-divisorã4ã®æ35ã4ã§å²ãåããªãã®ã§ ( else (find-divisor n (+ test-divisor 1))) test-divisorã5ã®æ35ã5ã§å²ãåããã®ã§ smallest-divisor 5
ä»åº¦ã¯å¦çããã¾ãå¤ãããªãã§ããã
ãã¼ãã»ã»ã»
ããããã£ã±ã
((> (square test-divisor) n) n)
ãã®ã³ã¼ãã«ã¯ä½ã®æå³ãããã®ã ããã
å¦çé度ããªã??
ã¡ãªã¿ã«ãã®ã¢ã«ã´ãªãºã ãå¦çã«é¢ãã¦ã¯æãã£ããã«ã¦æãã¦é ãã¾ãã
ãæ¬äººã®è¨±å¯ããã£ããæ¬æãæè¬ãè¾¼ãã¦ãååãå
¬è¡¨ãããã§ãâª
æ¸ãã¦ããã¡ãé·ããªãããã¦å
¬éãé
ããªã£ã¡ãã£ããªã(ï¾Ðï½ï¾)ï¾ã