読者です 読者をやめる 読者になる 読者になる

(wat-aro)

無職から有職者にランクアップしました

SICP 問題 3.72

sum-squareのところで間違えて立方数をたしていたせいで,
 

gosh> (stream-ref triple-way-sum-square-number 0)
87539319

って出てきて驚きました.

(define (sum-square x)
  (let ((a (car x))
        (b (cadr x)))
    (+ (* a a) (* b b))))

(define (triple-way-sum-square stream)
  (let ((s1 (stream-car stream))
        (s2 (stream-car (stream-cdr stream)))
        (s3 (stream-car (stream-cdr (stream-cdr stream)))))
    (let ((w1 (sum-square s1))
          (w2 (sum-square s2))
          (w3 (sum-square s3)))
      (cond ((= w1 w2 w3)
             (cons-stream w1
                          (triple-way-sum-square
                           (stream-cdr (stream-cdr stream)))))
            (else
             (triple-way-sum-square (stream-cdr stream)))))))

(define triple-way-sum-square-number
  (triple-way-sum-square (weighted-pairs integers integers sum-square)))

gosh> (stream-head triple-way-sum-square-number 20) 325 425 650 725 845 850 925 1025 1105 1250 1300 1325 1445 1450 1525 1625 1690 1700 1825 1850 done