39 lines
729 B
Scheme
39 lines
729 B
Scheme
(load "1_28.sch") ; for prime?
|
|
|
|
(define (filtered-accumulate combiner null-value pred term a next b)
|
|
(if (> a b)
|
|
null-value
|
|
(combiner
|
|
(if (pred a) (term a) null-value)
|
|
(filtered-accumulate combiner null-value pred term (next a) next b))))
|
|
|
|
(define (square x) (* x x))
|
|
|
|
(define (gcd a b)
|
|
(if (= b 0)
|
|
a
|
|
(gcd b (remainder a b))))
|
|
|
|
(define (rel-prime a b)
|
|
(= (gcd a b) 1))
|
|
|
|
(define (sum-squares-primes a b)
|
|
(filtered-accumulate
|
|
(lambda (x y) (+ x y))
|
|
0
|
|
prime?
|
|
square
|
|
a
|
|
(lambda (x) (+ 1 x))
|
|
b))
|
|
|
|
(define (prod-rel-prime n)
|
|
(filtered-accumulate
|
|
(lambda (x y) (* x y))
|
|
1
|
|
(lambda (x) (rel-prime x n))
|
|
(lambda (x) x)
|
|
1
|
|
(lambda (x) (+ 1 x))
|
|
n))
|