3.71 Exercise 3.71
In order to find integers that can be written as the sum of two cubes in two different ways as generally as possible, we can write a find-consecutive procedure that takes a stream and returns a stream of the values in the stream that appear consecutively. (In this case, I will take consecutively to mean two or more times in a row).
(define (weight-cubed p) (let ((cube (lambda (x) (* x x x)))) (+ (cube (car p)) (cube (cadr p)))))
(define (drop-while f s) (cond ((stream-null? s) the-empty-stream) ((f (stream-car s)) (drop-while f (stream-cdr s))) (else s)))
(define (find-consecutive s) (if (stream-null? s) the-empty-stream (let ((first (stream-car s)) (rest (stream-cdr s))) (if (stream-null? rest) the-empty-stream (let ((second (stream-car rest))) (if (= first second) (cons-stream first (find-consecutive (drop-while (lambda (x) (= x first)) (stream-cdr rest)))) (find-consecutive rest)))))))
(define ramanujan-pairs (find-consecutive (stream-map weight-cubed (weighted-pairs weight-cubed integers integers))))
We can then see the one number provided and the five after it:
> (display-stream (take-stream ramanujan-pairs 6))
1729
4104
13832
20683
32832
39312
'done