Solution to Exercise 1.33:

`(define (filtered-accumulate predicate combiner null-value term a next b)`

(define (iter a result)

(if (> a b)

result

(iter (next a) (if (predicate a)

(combiner (term a) result)

result))))

(iter a null-value))

```
```; part a

(define (square x) (* x x))

(define (sum-of-squared-primes a b)

(define (square-and-add i result) (+ (square i) result))

(filtered-accumulate prime? square-and-add 0 identity a inc b))

`; part b`

(define (product-of-positives-relatively-prime-to n)

(define (relatively-prime? i)

(= 1 (gcd i n)))

(filtered-accumulate relatively-prime? * 1 identity 1 inc (- n 1)))

Advertisements