这题没什么难度,just modify a little bit from the previos procedure (prime? n)
(define (runtime) (current-milliseconds)) (define (square x) (* x x)) (define (next-divisor n) (if (= n 2) 3 (+ n 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 (next-divisor test-divisor))))) (define (divides? a b) (= (remainder b a) 0)) (define (prime? n) (= n (smallest-divisor n))) (define (timed-prime-test n) (start-prime-test n (runtime))) (define (start-prime-test n start-time) (if (prime? n) (- (runtime) start-time))) (timed-prime-test 10000000019) (timed-prime-test 10000000033) (timed-prime-test 10000000061)
然后输出
594
523
600
previous version
(define (runtime) (current-milliseconds)) (define (square x) (* x x)) (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 (timed-prime-test n) (start-prime-test n (runtime))) (define (start-prime-test n start-time) (if (prime? n) (- (runtime) start-time))) (timed-prime-test 10000000019) (timed-prime-test 10000000033) (timed-prime-test 10000000061)
输出
1507
1029
1203
两倍的关系还是比较相近的