Skip to main content

Finding gcd using Euclid's Algorithms

According to Euclid algorithm if we have to gcd of a & b then you can get it recursively.The recursive function can be define as:

 

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



In the above definition of function gcd there are no restriction on passing  parameter a,b that a should be less that b or vice versa.

At the time of passing the parameter you pass b as smaller number you can save one unnecessary call.

If  d=gcd(a,b) then if there are a natural number e and 
                                               a%e==0 and b%e==0:
                               then                  d%e==0 must be true   


The above code spinet is in python. 
                           

Comments

Post a Comment