[ create a new paste ] login | about

Project: programmingpraxis
Link: http://programmingpraxis.codepad.org/pnV0GZKI    [ raw code | output | fork ]

programmingpraxis - Scheme, pasted on Oct 25:
; 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)


Output:
1
2
3
4
((12 35 37) (9 40 41) (20 21 29) (7 24 25) (8 15 17) (5 12 13) (3 4 5))
((3 4 5) (5 12 13) (7 24 25) (9 40 41) (20 21 29) (8 15 17) (12 35 37))
808950
808950


Create a new paste based on this one


Comments: