# 练习1.23-练习1.30

16 篇文章 0 订阅
15 篇文章 0 订阅

1.练习1.23

(define (find-divisor-better n a)
(cond ((> (square a) n) n)
((divides?-better n a) a)
(else (find-divisor-better n (next-2 a)))))

(define (next-2 n)
(if (= 2 n)
(+ 1 n)
(+ 2 n)))
(define (find-min-prime-since-better n start-time number)
(cond ((= number 0) (display-info start-time (real-time-clock)))
((prime? n) (begin
(format #t "~S~%" n)
(find-min-prime-since-better (+ n (next-plus n)) start-time (- number 1))))
(else (find-min-prime-since-better (+ n (next-plus n)) start-time number)))
#f)
(define (next-plus n)
(if (even? n)
1
2))

2.练习1.24

3.练习1.25

4.练习1.26

5.练习1.27

(define (try-it a n)
(= (expmod a n n) a))

(define (carmichael-test n number)
(cond ((= number 0) true)
((try-it number n) (carmichael-test n (- number 1)))
(else false)))

(define (car-prime? n)
(carmichael-text n (- n 1)))

6.练习1.28

(define (new-expmod base exp m)
(define tmpbase 100)
(cond ((= exp 0) 1)
((even? exp) (begin
(set! tmpbase (new-expmod base (/ exp 2) m))
(if (and (not (= tmpbase (- m 1)))
(not (= tmpbase 1))
(= (remainder (square tmpbase)
m) 1))
0
(remainder (square tmpbase)
m))))
(else (remainder (* base (new-expmod base (- exp 1) m))
m))))
(define (new-try-it n a)
(new-expmod a (- n 1) n))

(define (new-fermat-test n start)
(cond ((= start 1) true)
((= (new-try-it n start) 0) false)
(else (new-fermat-test n (- start 1)))))

(define (really-prime? n)
(new-fermat-test n (if (even? n)
(/ n 2)
(/ (- n 1) 2))))


7.练习1.29

(define (xps f a b n)
(define h
(/ (- b a) n))
(define (f1 k)
(f (+ a (* k h))))
(define (f2 k)
(if (or (= k 0) (= k n))
(f1 k)
(if (even? k)
(* 2 (f1 k))
(* 4 (f1 k)))))
(+ x 1))
(* (/ h 3)
(sum f2 0 add-h n)))

8.练习1.30

(define (sum term a next b)
(define (iter a result)
(if (> a b)
result
(iter (next a) (+ (term a) result))))
(iter a 0))

• 0
点赞
• 0
收藏
觉得还不错? 一键收藏
• 0
评论
09-19 186
08-06 2180
05-28 453
01-10 805
01-08 671
01-20 643
01-13 638
01-06 637
02-07 628

1.余额是钱包充值的虚拟货币，按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载，可以购买VIP、付费专栏及课程。