1

After some experimentation and search, I came up with the following definition:

emcd' :: Integer -> Integer -> (Integer,Integer,Integer)
emcd' a 0 = (a, 1, 0)
emcd' a b = 
  let (g, t, s) = emcd' b r
  in (g, s, t - (q * s))
    where
      (q, r) = divMod a b
  • What's the meaning behind this expression t - (q * s) ?

I've tried to evaluate it by hand; even though I arrived at the correct result (1, -4, 15), I can't see why that expression returns the value of t.

There is a famous method for calculating s and t in as + bt = gcd(a, b). In the process of finding the gcd, I get several equations.

By reversing the steps in the Euclidean Algorithm, it is possible to find these integers a and b. Those resulting equations look like the expression t - (q * s); however, I can't figure out the exact process.

1 Answer 1

3

Since (q, r) = divMod a b, we have the equation

a = qb + r

and because of the recursive call, we have:

tb + sr = g

Substituting a-qb for r in the second equation, that means

tb + s(a-qb) = g
tb + sa - qsb = g
sa + (t-qs)b = g

This explains why s and t - q*s are good choices to return.

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you so much for your answer. Could you explain this line, please ? "and because of the recursive call, we have: tb + sr = g"
@F.Zer That's exactly the property you stated in the question... "Calculating s and t in as + bt = gcd(a, b)"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.