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. 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
, weighted according to the sum
i³ + j³
), 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
. What are the next five?
There are no comments yet.
You must log in to post a comment.Login
(define (cube x) (* x x x)) (define (stream-car stream) (car stream)) (define (stream-cdr stream) (force (cdr stream))) (define (stream-map proc . list-of-stream) (if (null? (car list-of-stream)) '() (cons-stream (apply proc (map (lambda (s) (stream-car s)) list-of-stream)) (apply stream-map (cons proc (map (lambda (s) (stream-cdr s)) list-of-stream)))))) (define (add-streams s1 s2) (stream-map + s1 s2)) (define ones (cons-stream 1 ones)) (define integers (cons-stream 1 (add-streams ones integers))) (check-equal? (stream-car ramanujan-numbers) 1729) (check-equal? (stream-car (stream-cdr ramanujan-numbers)) 4104)