(define (unique-pairs n)
(accumulate
(lambda (x y)
(append (map (lambda (z)
(cons x z)) (enumerate-interval 1 (- x 1))) y))
nil (enumerate-interval 1 n)))
(define (prime-sum-pairs n)
(filter prime-sum?
(map
(lambda (x)
(list (car x) (cdr x) (+ (car x) (cdr x))))
(unique-pairs n))))
2.41
;this generates ordered triples of distinct positive
;integers i, j, and k less than or equal to a given
;integer n
(define (enumerate-all-triplets n)
(flatmap (lambda (x) x)
(map
(lambda (x)
(map (lambda (y)
(list y (car x) (cdr x)))
(enumerate-interval (+ (car x) 1) n)))
(unique-pairs n))))
(define (triplet-with-sum-s n s)
(filter (lambda (x)
(= s
(+ (car x) (cadr x) (caddr x))))
(enumerate-all-triplets n)))
No comments:
Post a Comment