Binary GCD algorithm also known as Stein's algorithm is an improved version of Euclidean Algorithm.It takes two integer inputs and finds the Greatest Common Divisor(GCD) of the two numbers. The algorithm uses recursive techniques to find the GCD of the two numbers.
Program
This program doesn't accept negative numbers at all. If you give a negative number as input then you will get an error during runtime. This program doesn't accept negative numbers as input. If the user gives one or both the negative numbers then a runtime error is raised.We could have added
u = abs(u)
and v = abs(v)
, but this will have an effect on the performance of the program. As we are using recursive program, for each and every function call the above two statements gets executed which is not at all necessary. To find the GCD of two numbers, there are 8 cases. They are as follows:(Assume that the input is two integers
u
and v
, gcd
is the name of our function)-
u
andv
are the same. For this condition, the GCD is the same as any of the two numbers. - Value of
u
is0
. Then the GCD isv
. - Value of
v
is 0. GCD will beu
. u
is evenv
is even. Then the GCD of the two numbers will be2*gcd(u/2, v/2)
(Because 2 is the common factor). So here we are using the recursive function call.u
is evenv
is odd. In this case GCD will begcd(u/2, v)
(Because 2 is never gonna make into the value of GCD). Again a recursive functional call.u
is oddv
is even. GCD will begcd(u, v/2)
(Similar to the above case).u
is oddv
is odd andu > v
. Then the GCD will begcd((u-v)/2, v)
. This case is to ensure that the input given to thegcd
function will not be a negative integer.u
is oddv
is odd andu < v
. GCD will begcd((v-u)/2, u)
. This case is similar to the above one.
variable & 1
gives0
when the number is even and1
when the number is odd.variable >> 1
divides the number by 2.variable << 1
multiplies the number by 2
These three operations execute their respective arithmetic in an efficient manner.
Non recursive version
Till now we have seen the recursive program which uses the same function again and again to return the final value. But the following program will only use loops to find the value. The program is as follows:The program is very much self explanatory, but I will give a brief description about what actually is happening in the background.
We will take
u
and v
and find the highest power of 2 common between them. For example if 4 and 32 are taken then the highest power of 2 common between them is 22. After that we will remove all the excess powers of 2's in
u
and v
which will however not contribute to the final answer.Now we only have two odd numbers and you can see the code implementation of cases 6 and 7 in the program.
From python 2.6, there is an inbuilt
gcd
function which will calculate the GCD of two numbers. You can import the function and use it directly.As always you can download the file from here: Github Gist binary_gcd.py, Github Gist binary_gcd_nr.py and Github Gist fractions_gcd.py.
If you want to learn more about the GCD algorithm, then you can visit the following websites:
GCD algorithm Wiki
binary GCD
Binary GCD mathematical Garden
GCD - Rosetta Code
If you have any doubt or haven't understood anything feel free to comment in the comment box and I will be glad to help you.
If you have any suggestions or have a different implementation of the program then please comment in the comment box below and I will be very happy to see your comment.
Please comment if you have found any typo or execution errors if any.
You can also contact me
Thank you. Have a nice day.