1

I am trying to calculate GCD of two numbers using Euclidean algorithm. I am following this resource.

Euclidean algorithm says that we nee to keep on subtracting smaller number from larger until the two numbers become same. Instead of subtracting they are using mod in this resource.

  1. First version

     def gcd(a, b):  
         if a == 0 : 
             return b  
         return gcd(b%a, a)
    
  2. Second version

     def gcd(a, b):
         if (b == 0):
             return a
         return gcd(b, (a % b))
    

In both version, the code are different but it gives me same result.I don't understand how this is working. Can someone explain me?

2 Answers 2

2

Each implementation of gcd is the mirror image of the other.

In the second example, if the variables a and b were swapped, you would get the first version. Hence these gcd implementations are equivalent. The only thing that changes, is the order of a and b.

This algorithms is know as Euclidean division. From Wikipedia:

At every step k, the Euclidean algorithm computes a quotient qk and remainder rk from two numbers rk-1 and rk-2

rk-2 = qk rk-1 + rk

where the rk is non-negative and is strictly less than the absolute value of rk-1. The theorem which underlies the definition of the Euclidean division ensures that such a quotient and remainder always exist and are unique.

In Euclid's original version of the algorithm, the quotient and remainder are found by repeated subtraction; that is, rk-1 is subtracted from rk-2 repeatedly until the remainder rk is smaller than rk-1. After that rk and rk-1 are exchanged and the process is iterated. Euclidean division reduces all the steps between two exchanges into a single step, which is thus more efficient. Moreover, the quotients are not needed, thus one may replace Euclidean division by the modulo operation, which gives only the remainder. Thus the iteration of the Euclidean algorithm becomes simply

rk = rk-2 mod rk-1

See also

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

2 Comments

Thanks.Can you explain any one version?
@Gnanavel See updated answer with explanation of the Euclidean division algorithm.
2
def gcd1(a, b):  
    if a == 0 : 
        return b  
    return gcd(b%a, a)

def gcd2(a, b):
    if (b == 0):
        return a
    return gcd(b, (a % b))

The first function and the second function are exactly the same. The only difference is that the arguments a and b are swapped. Since the Euclidean Algorithm works with any order of inputs, the functions are exactly the same.

2 Comments

Euclidean says that we need to subtract smaller from larger.But how the mod works?I really don't unserstand.Whether we need to take mod of larger with smaller or smaller with larger.Can you explain me?
@Gnanavel If, for example, the larger number was passed to gcd1 first, then b%a would evaluate to b (the smaller number) and therefore the next recursive call to gcd1 would be gcd1(b, a), reversing the order of the arguments and therefore ignoring the order.

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.