-
Notifications
You must be signed in to change notification settings - Fork 10
/
1.33.ss
38 lines (31 loc) · 1.04 KB
/
1.33.ss
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
(define (filtered-accumulate filter combiner null-value term a next b)
(cond ((> a b) null-value)
((filter a)
(combiner (term a)
(filtered-accumulate filter combiner
null-value term (next a) next b)))
(else
(filtered-accumulate filter combiner
null-value term (next a) next b))))
(define (smallest-divisor n)
(find-divisor n 2))
(define (square n) (* n n))
(define (divides? a b)
(= (remainder b a) 0))
(define (next test-divisor)
(if (= test-divisor 2)
3
(+ test-divisor 2)))
(define (find-divisor n test-divisor)
(cond ((> (square test-divisor) n) n)
((divides? test-divisor n) test-divisor)
(else (find-divisor n (next test-divisor)))))
(define (prime? n)
(if (= n (smallest-divisor n))
n
(smallest-divisor n)))
(define (sum-of-prime a b)
(define (sum-term n) n)
(define (sum-next n) (+ n 1))
(filtered-accumulate prime? + 0 sum-term a sum-next b))
(sum-of-prime 15 100)