SICP問題3.71

立方数の和で重み付けするための手続き

(define (cube-weight x)
  (let ((i (car x))
        (j (cadr x)))
    (+ (cube i) (cube j))))

立方数の和で重み付けした順序のストリーム

(define cube-weighted-stream
  (weighted-pairs integers integers cube-weight))

二通り以上の二つの立方数の和で表される数Ramanujan数を生成する手続き

(define (ramanujan-sub s)
  (let ((s-weight (cube-weight (stream-car s)))
        (t-weight (cube-weight (stream-car (stream-cdr s)))))
    (if (= s-weight t-weight)
        (cons-stream s-weight
                     (ramanujan-sub (stream-cdr s)))
        (ramanujan-sub (stream-cdr s)))))
(define ramanujan-stream
  (ramanujan-sub cube-weighted-stream))

Ramajanun数(最初の6個)

(stream-ref-range ramanujan-stream 0 5)
; 1729
; 4104
; 13832
; 20683
; 32832
; 39312
; #<undef>