Exercise 3.71. Numbers that can be expressed as the sum of two cubes in more than one way are sometimes called Ramanujan numbers, in honor of the mathematician Srinivasa Ramanujan.70 Ordered streams of pairs provide an elegant solution to the problem of computing these numbers. To find a number that can be written as the sum of two cubes in two different ways, we need only generate the stream of pairs of integers (i,j) weighted according to the sum i3 + j3 (see exercise 3.70), then search the stream for two consecutive pairs with the same weight. Write a procedure to generate the Ramanujan numbers. The first such number is 1,729. What are the next five? ———————————————————————————————————————————————————————————————————————— (define (cube-sum pair) (+ (expt (car pair) 3) (expt (cadr pair) 3))) (define pairs-by-cube-sum (weighted-pairs integers integers cube-sum)) (define (weight-equal stream weight) (define (go stream pair target) (let* ((next-pair (stream-car stream)) (next-weight (weight next-pair))) (if (= next-weight target) (cons-stream (list pair next-pair) (go (stream-cdr stream) next-pair next-weight)) (go (stream-cdr stream) next-pair next-weight)))) (go (stream-cdr stream) (stream-car stream) (weight (stream-car stream)))) (define ramanujan-pairs (weight-equal pairs-by-cube-sum cube-sum)) Now we can find the pairs: 1 ]=> (display-stream-n ramanujan-pairs 6) ((9 10) (1 12)) ((9 15) (2 16)) ((18 20) (2 24)) ((19 24) (10 27)) ((18 30) (4 32)) ((15 33) (2 34)) And the first six Ramanujan numbers are: 1 ]=> (map cube-sum '((9 10) (9 15) (18 20) (19 24) (18 30) (15 33))) ;Value 14: (1729 4104 13832 20683 32832 39312)