-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathex1.33-filtered-accumulate.scm
56 lines (43 loc) · 1.31 KB
/
ex1.33-filtered-accumulate.scm
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
(define (accumulate-iter combiner null-value term a next b)
(define (iter a result)
(if (> a b)
result
(iter (next a) (combiner a result))))
(iter a null-value))
(define (filtered-accumulate filter combiner null-value term a next b)
(define (iter a result)
(cond ((> a b) result)
((not (filter a)) (iter (next a) result))
(else (iter (next a) (combiner (term a) result)))))
(iter a null-value))
(define (inc n) (+ n 1))
(define (identity x) x)
(define (square x) (* x x))
(define (remainder a b)
(define (iter x)
(if (> (* x b) a)
(- a (* (- x 1) b))
(iter (+ x 1))))
(iter 1))
(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)
(and (= n (smallest-divisor n)) (not (= n 1))))
(define (sum-prime-square a b)
(filtered-accumulate prime? + 0 square a inc b))
(sum-prime-square 1 5) ;38
(define (gcd a b)
(if (= b 0)
a
(gcd b (remainder a b))))
(define (product-relative-prime n)
(define (relative-prime? x)
(= 1 (gcd x n)))
(filtered-accumulate relative-prime? * 1 identity 1 inc n))
(product-relative-prime 10) ; 189