Fixed bug 14177
bezout-binary did not handle negative numbers correctly.
This commit is contained in:
parent
8f6af94221
commit
0909acc473
|
@ -42,9 +42,29 @@
|
|||
(if (= r 0)
|
||||
(list ub vb)
|
||||
(loop b r ub vb (- ua (* q ub)) (- va (* q vb))))))
|
||||
(if (> a b)
|
||||
(loop a b 1 0 0 1)
|
||||
(loop b a 0 1 1 0)))
|
||||
(: start : Integer Integer -> (List Integer Integer))
|
||||
(define (start a b)
|
||||
(if (> a b)
|
||||
(loop a b 1 0 0 1)
|
||||
(loop b a 0 1 1 0)))
|
||||
(cond [(and (positive? a) (positive? b))
|
||||
(start a b)]
|
||||
[(and (negative? a) (negative? b))
|
||||
(define uv (start (- a) (- b)))
|
||||
(list (- (car uv)) (- (cadr uv)))]
|
||||
[(and (negative? a) (positive? b))
|
||||
; choose k s.t. a+kb>0
|
||||
(define k (+ (quotient (- a) b) 1))
|
||||
(define uv (start (+ a (* k b)) b))
|
||||
(define u (car uv)) (define v (cadr uv))
|
||||
(list u (+ (* u k) v))]
|
||||
[(and (positive? a) (negative? b))
|
||||
; choose k s.t. ak+b>0
|
||||
(define k (+ (quotient (- b) a) 1))
|
||||
(define uv (start a (+ (* k a) b)))
|
||||
(define u (car uv)) (define v (cadr uv))
|
||||
(list (+ u (* k v)) v)]
|
||||
[else (error "Internal error in bezout-binary")]))
|
||||
|
||||
; (bezout a b c ...) -> (list u v w ...) <=> gcd(a,b,c,...) = au + bv + cw + ...
|
||||
(: bezout : Integer Integer * -> (Listof Integer))
|
||||
|
|
Loading…
Reference in New Issue
Block a user