SICP 1.2.6 素数检测

来源:互联网 发布:网络电影疯狂小镇 编辑:程序博客网 时间:2024/06/01 21:47

O(n)的复杂度

(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 (divides? a b)  (= (remainder b a) 0))(define (prime? n)  (= n (smallest-divisor n)))(define (square x) (* x x)) 
0 0
原创粉丝点击