fork download
  1. ; tri-48
  2.  
  3. (define (isqrt n)
  4. (if (not (and (positive? n) (integer? n)))
  5. (error 'isqrt "must be positive integer")
  6. (let loop ((x n))
  7. (let ((y (quotient (+ x (quotient n x)) 2)))
  8. (if (< y x) (loop y) x)))))
  9.  
  10. (define (square? n)
  11. (let ((root (isqrt n)))
  12. (= (* root root) n)))
  13.  
  14. (define (squares n)
  15. (let loop ((x (isqrt n)) (y 0) (zs '()))
  16. (cond ((< x y) zs)
  17. ((< (+ (* x x) (* y y)) n) (loop x (+ y 1) zs))
  18. ((< n (+ (* x x) (* y y))) (loop (- x 1) y zs))
  19. (else (loop (- x 1) (+ y 1) (cons (list x y) zs))))))
  20.  
  21. (display (squares (* 48 48))) (newline)
  22.  
  23. (define (tri n)
  24. (let loop ((a 1))
  25. (let ((c (+ (* a a) (* n n))))
  26. (when (square? c)
  27. (display (sort (list a (isqrt c) n) <))
  28. (newline)))
  29. (when (< a 1000) (loop (+ a 1)))))
  30.  
  31. (tri 48)
Success #stdin #stdout 0.09s 43488KB
stdin
Standard input is empty
stdout
((48 0))
(14 48 50)
(20 48 52)
(36 48 60)
(48 55 73)
(48 64 80)
(48 90 102)
(48 140 148)
(48 189 195)
(48 286 290)
(48 575 577)