Wednesday, September 23, 2015

Extended Euclidean Algorithm with negative numbers minimum non-negative solution


I came through a problem in programming which needs Extended Euclidean Algorithm, with as+bt=gcd(|a|,|b|) for b0 and a0


With the help of this post: extended-euclidean-algorithm-with-negative-numbers


I know we can just move the sign towards t and just use normal Extended Euclidean Algorithm and use (s,t) as solution



However in my scenario, there is one more condition: I would like to find the minimum non-negative solution, i.e. (s,t) for s,t0


And my question is how to find such minimum (s,t)?


Sorry if it sounds too obvious as I am dumb :(


Thanks!


Answer



Fact 1: One nice property of the Extended Euclidean Algorithm is that it already gives minimal solution pairs, that is, if a,b0, |s|<bgcd(a,b) and |t|<agcd(a,b)


Fact 2: If (s,t) is a solution then (s+kbgcd(a,b),tkagcd(a,b)),kZ is also a solution.


Combining the two facts above, for your case in which a0 and b0, compute the pair (s,t) using the extended algorithm on (|a|,|b|), then either:


  1. s0,t0, in this case (s,t) is the solution you want.

  2. s0,t0, in this case (s+|b|gcd(|a|,|b|),t+|a|gcd(|a|,|b|)) is your desired solution.


No comments:

Post a Comment

analysis - Injection, making bijection

I have injection f:AB and I want to get bijection. Can I just resting codomain to f(A)? I know that every function i...