[ create a new paste ] login | about

Link: http://codepad.org/kdQgtQ34    [ raw code | output | fork ]

programmingpraxis - Scheme, pasted on Jan 4:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
; the sum of two squares

(define (isqrt n)
  (let loop ((x n) (y (quotient (+ n 1) 2)))
    (if (<= 0 (- y x) 1) x
      (loop y (quotient (+ y (quotient n y)) 2)))))

(define (squares n)
  (let loop ((x (isqrt n)) (y 0) (zs '()))
    (cond ((< x y) zs)
          ((< (+ (* x x) (* y y)) n) (loop x (+ y 1) zs))
          ((< n (+ (* x x) (* y y))) (loop (- x 1) y zs))
          (else (loop (- x 1) (+ y 1) (cons (list x y) zs))))))

(display (squares 50)) (newline)
(display (squares 48612265)) (newline)
(display (squares 999)) (newline)


Output:
1
2
3
((5 5) (7 1))
((5008 4851) (5139 4712) (5179 4668) (5243 4596) (5432 4371) (5613 4136) (5656 4077) (5691 4028) (5832 3821) (5907 3704) (6048 3469) (6124 3333) (6213 3164) (6259 3072) (6384 2803) (6404 2757) (6413 2736) (6556 2373) (6576 2317) (6637 2136) (6651 2092) (6756 1723) (6772 1659) (6789 1588) (6853 1284) (6899 1008) (6917 876) (6944 627) (6948 581) (6952 531) (6971 132) (6972 59))
()


Create a new paste based on this one


Comments: