; pythagorean triples
(define (euclid limit)
(define (trip m n)
(let ((m2 (* m m)) (n2 (* n n)))
(let ((a (- m2 n2)) (b (* 2 m n)) (c (+ m2 n2)))
(if (< a b) (list a b c) (list b a c)))))
(let m-loop ((m 1) (trips (list)))
(if (< limit (* m m)) trips
(let n-loop ((n (+ (modulo m 2) 1)) (trips trips))
(if (<= m n) (m-loop (+ m 1) trips)
(let* ((t (trip m n)) (p (apply + t)))
(if (< limit p)
(m-loop (+ m 1) trips)
(n-loop (+ n 2) (if (= (gcd (car t) (cadr t)) 1)
(cons t trips) trips)))))))))
(display (euclid 100)) (newline)
(define (hall n)
(let loop ((a 3) (b 4) (c 5))
(if (< n (+ a b c)) (list)
(append
(list (if (< a b) (list a b c) (list b a c)))
(loop (+ a (- b) (- b) c c)
(+ a a (- b) c c)
(+ a a (- b) (- b) c c c))
(loop (+ a b b c c)
(+ a a b c c)
(+ a a b b c c c))
(loop (+ (- a) b b c c)
(+ (- a) (- a) b c c)
(+ (- a) (- a) b b c c c))))))
(display (hall 100)) (newline)
(define (f pyth n)
(apply +
(map (lambda (p) (quotient n p))
(map (lambda (xs) (apply + xs))
(pyth n)))))
(display (f euclid 1000000)) (newline)
(display (f hall 1000000)) (newline)