You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
45 lines
893 B
45 lines
893 B
|
|
Exercise 1.33: You can obtain an even more
|
|
general version of accumulate (Exercise 1.32) by introducing the
|
|
notion of a
|
|
filter on the terms to be combined. That is, combine
|
|
only those terms derived from values in the range that satisfy a specified
|
|
condition. The resulting filtered-accumulate abstraction takes the same
|
|
arguments as accumulate, together with an additional predicate of one argument
|
|
that specifies the filter. Write filtered-accumulate as a procedure.
|
|
Show how to express the following using filtered-accumulate:
|
|
|
|
|
|
the sum of the squares of the prime numbers in the interval
|
|
a
|
|
to
|
|
b
|
|
|
|
(assuming that you have a prime? predicate already written)
|
|
|
|
the product of all the positive integers less than
|
|
n
|
|
that are relatively
|
|
prime to
|
|
n
|
|
(i.e., all positive integers
|
|
|
|
i
|
|
<
|
|
n
|
|
|
|
such that
|
|
|
|
|
|
GCD
|
|
(
|
|
i
|
|
,
|
|
n
|
|
)
|
|
=
|
|
1
|
|
|
|
).
|
|
|
|
|
|
|