; (egcd a b) => (x . y) such that ax+by = (gcd a b) (lambda (a b) (let egcd ((a a) (b b)) (if (= b 0) '(1 . 0) (let* ((q (quotient a b)) (r (- a (* q b)))) (if (zero? r) '(0 . 1) (let ((c (egcd b r))) (cons (cdr c) (- (car c) (* (cdr c) q))))))))) ; tests (define t (let ((E (fileVal "egcd"))) (lambda (a b) (let ((e (E a b))) (list e (gcd a b) (+ (* a (car e)) (* b (cdr e)))))))) (t (* 641 97) (* 641 211)) ; => ((-87 . 40) 641 641) (t (* 12376577 2357) (* 12376577 2371)) ; => ((508 . -505) 12376577 12376577)