-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathextended-euclidean-algorithm.sls
43 lines (30 loc) · 1.03 KB
/
extended-euclidean-algorithm.sls
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#!r6rs
(library (mpl extended-euclidean-algorithm)
(export extended-euclidean-algorithm)
(import (mpl rnrs-sans)
(mpl arithmetic)
(mpl leading-coefficient-gpe)
(mpl algebraic-expand)
(mpl polynomial-division))
(define (extended-euclidean-algorithm u v x)
(if (and (equal? u 0)
(equal? v 0))
(list 0 0 0)
(let loop ((u u)
(v v)
(App 1)
(Ap 0)
(A #f)
(Bpp 0)
(Bp 1)
(B #f))
(if (equal? v 0)
(let ((c (leading-coefficient-gpe u x)))
(list (algebraic-expand (/ u c))
(algebraic-expand (/ App c))
(algebraic-expand (/ Bpp c))))
(let ((q (quotient u v x))
(r (remainder u v x)))
(let ((A (- App (* q Ap)))
(B (- Bpp (* q Bp))))
(loop v r Ap A A Bp B B))))))))