-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsol-1.33.tex
More file actions
26 lines (26 loc) · 843 Bytes
/
Copy pathsol-1.33.tex
File metadata and controls
26 lines (26 loc) · 843 Bytes
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
Here is a `filtered-accumulate` procedure (that generates an iterative process):
\begtt\scm
(define (filtered-accumulate filter combiner null-value term a next b)
(define (iter a result)
(if (> a b)
result
(iter (next a)
(if (filter a)
(combiner result (term a))
result))))
(iter a null-value))
\endtt
\begitems\style a
* The sum of the squares of the prime numbers in the interval $a$ to $b$ can be expressed by
\begtt\scm
(define (s a b)
(filtered-accumulate prime? + 0 square a inc b))
\endtt
* The product of all positive integers less than $n$ that are relatively prime to $n$ can be expressed by
\begtt\scm
(define (p n)
(define (relatively-prime? i)
(= 1 (gcd i n)))
(filtered-accumulate relatively-prime? * 1 identity 1 inc (- n 1)))
\endtt
\enditems