【发布时间】:2015-11-30 20:18:34
【问题描述】:
以下程序找到给定数字n 的最小整数除数(大于1)。它以一种简单的方式做到这一点,通过测试n 是否可以被以2 开头的连续整数整除。
n 是素数当且仅当n 是它自己的最小除数。
(define (square x) (* x x))
(define (divisible? a b)
(= (remainder a b) 0))
(define (find-divisor n test)
(cond ((> (square test) n) n)
((divisible? n test) test)
(else (find-divisor n (+ test 1)))))
(define (smallest-divisor n)
(find-divisor n 2))
(define (prime? n)
(= (smallest-divisor n) n))
如何编写一个程序来检查指定范围内连续奇数的素数?
(define (search_for_primes from to)
(cond ((> from to) false)
((prime? from) (display from))
(else (search_for_primes (+ 1 from) to))))
我的解决方案只是将1 写入输出。
【问题讨论】: