-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexercise1-33.scm
78 lines (60 loc) · 1.57 KB
/
exercise1-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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#lang scheme
;accumulate procedure with an additional filter argument.
;a recursive filtered-accumulate
(define (filtered-accumulate filter combine null-value term a next b)
(if (> a b)
null-value
(combine
(if (filter a)
(term a)
null-value)
(filtered-accumulate filter combine null-value term (next a) next b))))
;smallest divisor (improved to exclude even numbers)
(define (smallest-divisor n)
(find-divisor n 2))
(define (square x) (* x x))
(define (next n)
(cond ((= n 2) 3)
(else (+ n 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 (divides? a b)
(= (remainder b a) 0))
(define (prime? n)
(= n (smallest-divisor n)))
;the sum procedure
(define (sum filter term a next b)
(filtered-accumulate filter + 0 term a next b))
;increment procedure
(define (inc n) (+ n 1))
;sum of cubes
(define (sum-squares-prime a b)
(sum prime? square a inc b))
;test
(sum-squares-prime 2 10)
(+
(square 2)
(square 3)
(square 5)
(square 7))
;the product procedure
(define (product filter term a next b)
(filtered-accumulate filter * 1 term a next b))
;gcd
(define (gcd a b)
(if (= b 0)
a
(gcd b (remainder a b))))
;identity
(define (identity x) x)
(define (product-relative-prime n)
;check relative prime
(define (relative-prime? i)
(if (= (gcd i n) 1)
#t
#f))
(product relative-prime? identity 1 inc n))
(product-relative-prime 10)
(* 1 3 7 9)