Exercise 2.41 of SICP

Exercise 2.41: Write a procedure to find all ordered triples of distinct positive integers i, j, and k less than or equal to a given integer n that sum to a given integer s.

> (sum-triples 5 9)
((4 3 2 9) (5 3 1 9))
> (sum-triples 5 10)
((5 3 2 10) (5 4 1 10))
> (sum-triples 5 11)
((5 4 2 11))
> (sum-triples 5 12)
((5 4 3 12))
> (sum-triples 5 13)
()
> (sum-triples 6 13)
((6 4 3 13) (6 5 2 13))
> (sum-triples 6 10)
((5 3 2 10) (5 4 1 10) (6 3 1 10))
> (sum-triples 6 11)
((5 4 2 11) (6 3 2 11) (6 4 1 11))
> (sum-triples 6 12)
((5 4 3 12) (6 4 2 12) (6 5 1 12))
> (sum-triples 6 13)
((6 4 3 13) (6 5 2 13))

Leave a Reply