-
Notifications
You must be signed in to change notification settings - Fork 0
/
1.33.scm
57 lines (46 loc) · 1.31 KB
/
1.33.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
56
57
(define (filtered-accumulate combiner null-value term a next b filter)
(if (> a b)
null-value
(if (filter a)
(combiner (term a)
(filtered-accumulate combiner null-value
term (next a) next b filter))
(combiner null-value
(filtered-accumulate combiner null-value
term (next a) next b filter)))))
;; a)
(define (prime? n)
(= n (smallest-divisor n)))
(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 (square x)
(* x x))
(define (sum-primes a b)
(define (next x)
(+ x 1))
(define (term x)
x)
(filtered-accumulate + 0 term a next b prime?))
(sum-primes 0 10)
(+ 1 2 3 5 7)
;; b)
(define (gcd~ a b)
(if (= b 0)
a
(gcd~ b (remainder a b))))
(define (product-primes n)
(define (next x)
(+ x 1))
(define (term x)
x)
(define (filter x)
(= 1 (gcd~ x n)))
(filtered-accumulate * 1 term 1 next n filter))
(product-primes 10)
(* 1 3 7 9)