Stein’s Algorithm for finding GCD
Last Updated :
04 Jul, 2022
Stein’s algorithm or binary GCD algorithm is an algorithm that computes the greatest common divisor of two non-negative integers. Stein’s algorithm replaces division with arithmetic shifts, comparisons, and subtraction.
Examples:
Input: a = 17, b = 34
Output : 17
Input: a = 50, b = 49
Output: 1
Algorithm to find GCD using Stein’s algorithm gcd(a, b)
- If both a and b are 0, gcd is zero gcd(0, 0) = 0.
- gcd(a, 0) = a and gcd(0, b) = b because everything divides 0.
- If a and b are both even, gcd(a, b) = 2*gcd(a/2, b/2) because 2 is a common divisor. Multiplication with 2 can be done with bitwise shift operator.
- If a is even and b is odd, gcd(a, b) = gcd(a/2, b). Similarly, if a is odd and b is even, then
gcd(a, b) = gcd(a, b/2). It is because 2 is not a common divisor.
- If both a and b are odd, then gcd(a, b) = gcd(|a-b|/2, b). Note that difference of two odd numbers is even
- Repeat steps 3–5 until a = b, or until a = 0. In either case, the GCD is power(2, k) * b, where power(2, k) is 2 raise to the power of k and k is the number of common factors of 2 found in step 3.
Iterative Implementation
C++
#include <bits/stdc++.h>
using namespace std;
int gcd( int a, int b)
{
if (a == 0)
return b;
if (b == 0)
return a;
int k;
for (k = 0; ((a | b) & 1) == 0; ++k)
{
a >>= 1;
b >>= 1;
}
while ((a & 1) == 0)
a >>= 1;
do
{
while ((b & 1) == 0)
b >>= 1;
if (a > b)
swap(a, b);
b = (b - a);
} while (b != 0);
return a << k;
}
int main()
{
int a = 34, b = 17;
printf ( "Gcd of given numbers is %d\n" , gcd(a, b));
return 0;
}
|
Java
import java.io.*;
class GFG {
static int gcd( int a, int b)
{
if (a == 0 )
return b;
if (b == 0 )
return a;
int k;
for (k = 0 ; ((a | b) & 1 ) == 0 ; ++k)
{
a >>= 1 ;
b >>= 1 ;
}
while ((a & 1 ) == 0 )
a >>= 1 ;
do
{
while ((b & 1 ) == 0 )
b >>= 1 ;
if (a > b)
{
int temp = a;
a = b;
b = temp;
}
b = (b - a);
} while (b != 0 );
return a << k;
}
public static void main(String args[])
{
int a = 34 , b = 17 ;
System.out.println( "Gcd of given "
+ "numbers is " + gcd(a, b));
}
}
|
Python3
def gcd(a, b):
if (a = = 0 ):
return b
if (b = = 0 ):
return a
k = 0
while (((a | b) & 1 ) = = 0 ):
a = a >> 1
b = b >> 1
k = k + 1
while ((a & 1 ) = = 0 ):
a = a >> 1
while (b ! = 0 ):
while ((b & 1 ) = = 0 ):
b = b >> 1
if (a > b):
temp = a
a = b
b = temp
b = (b - a)
return (a << k)
a = 34
b = 17
print ( "Gcd of given numbers is " , gcd(a, b))
|
C#
using System;
class GFG {
static int gcd( int a, int b)
{
if (a == 0)
return b;
if (b == 0)
return a;
int k;
for (k = 0; ((a | b) & 1) == 0; ++k)
{
a >>= 1;
b >>= 1;
}
while ((a & 1) == 0)
a >>= 1;
do
{
while ((b & 1) == 0)
b >>= 1;
if (a > b) {
int temp = a;
a = b;
b = temp;
}
b = (b - a);
} while (b != 0);
return a << k;
}
public static void Main()
{
int a = 34, b = 17;
Console.Write( "Gcd of given "
+ "numbers is " + gcd(a, b));
}
}
|
PHP
<?php
function gcd( $a , $b )
{
if ( $a == 0)
return $b ;
if ( $b == 0)
return $a ;
$k ;
for ( $k = 0; (( $a | $b ) & 1) == 0; ++ $k )
{
$a >>= 1;
$b >>= 1;
}
while (( $a & 1) == 0)
$a >>= 1;
do
{
while (( $b & 1) == 0)
$b >>= 1;
if ( $a > $b )
swap( $a , $b );
$b = ( $b - $a );
} while ( $b != 0);
return $a << $k ;
}
$a = 34; $b = 17;
echo "Gcd of given numbers is " .
gcd( $a , $b );
?>
|
Javascript
<script>
function gcd( a, b)
{
if (a == 0)
return b;
if (b == 0)
return a;
let k;
for (k = 0; ((a | b) & 1) == 0; ++k)
{
a >>= 1;
b >>= 1;
}
while ((a & 1) == 0)
a >>= 1;
do
{
while ((b & 1) == 0)
b >>= 1;
if (a > b){
let t = a;
a = b;
b = t;
}
b = (b - a);
} while (b != 0);
return a << k;
}
let a = 34, b = 17;
document.write( "Gcd of given numbers is " + gcd(a, b));
</script>
|
Output
Gcd of given numbers is 17
Time Complexity: O(N*N)
Auxiliary Space: O(1)
Recursive Implementation
C++
#include <bits/stdc++.h>
using namespace std;
int gcd( int a, int b)
{
if (a == b)
return a;
if (a == 0)
return b;
if (b == 0)
return a;
if (~a & 1)
{
if (b & 1)
return gcd(a >> 1, b);
else
return gcd(a >> 1, b >> 1) << 1;
}
if (~b & 1)
return gcd(a, b >> 1);
if (a > b)
return gcd((a - b) >> 1, b);
return gcd((b - a) >> 1, a);
}
int main()
{
int a = 34, b = 17;
printf ( "Gcd of given numbers is %d\n" , gcd(a, b));
return 0;
}
|
Java
import java.io.*;
class GFG {
static int gcd( int a, int b)
{
if (a == b)
return a;
if (a == 0 )
return b;
if (b == 0 )
return a;
if ((~a & 1 ) == 1 )
{
if ((b & 1 ) == 1 )
return gcd(a >> 1 , b);
else
return gcd(a >> 1 , b >> 1 ) << 1 ;
}
if ((~b & 1 ) == 1 )
return gcd(a, b >> 1 );
if (a > b)
return gcd((a - b) >> 1 , b);
return gcd((b - a) >> 1 , a);
}
public static void main(String args[])
{
int a = 34 , b = 17 ;
System.out.println( "Gcd of given"
+ "numbers is " + gcd(a, b));
}
}
|
Python3
def gcd(a, b):
if (a = = b):
return a
if (a = = 0 ):
return b
if (b = = 0 ):
return a
if ((~a & 1 ) = = 1 ):
if ((b & 1 ) = = 1 ):
return gcd(a >> 1 , b)
else :
return (gcd(a >> 1 , b >> 1 ) << 1 )
if ((~b & 1 ) = = 1 ):
return gcd(a, b >> 1 )
if (a > b):
return gcd((a - b) >> 1 , b)
return gcd((b - a) >> 1 , a)
a, b = 34 , 17
print ( "Gcd of given numbers is " ,
gcd(a, b))
|
C#
using System;
class GFG {
static int gcd( int a, int b)
{
if (a == b)
return a;
if (a == 0)
return b;
if (b == 0)
return a;
if ((~a & 1) == 1) {
if ((b & 1) == 1)
return gcd(a >> 1, b);
else
return gcd(a >> 1, b >> 1) << 1;
}
if ((~b & 1) == 1)
return gcd(a, b >> 1);
if (a > b)
return gcd((a - b) >> 1, b);
return gcd((b - a) >> 1, a);
}
public static void Main()
{
int a = 34, b = 17;
Console.Write( "Gcd of given"
+ "numbers is " + gcd(a, b));
}
}
|
PHP
<?php
function gcd( $a , $b )
{
if ( $a == $b )
return $a ;
if ( $a == 0)
return $b ;
if ( $b == 0)
return $a ;
if (~ $a & 1)
{
if ( $b & 1)
return gcd( $a >> 1, $b );
else
return gcd( $a >> 1, $b >> 1) << 1;
}
if (~ $b & 1)
return gcd( $a , $b >> 1);
if ( $a > $b )
return gcd(( $a - $b ) >> 1, $b );
return gcd(( $b - $a ) >> 1, $a );
}
$a = 34; $b = 17;
echo "Gcd of given numbers is: " ,
gcd( $a , $b );
?>
|
Javascript
<script>
function gcd(a, b)
{
if (a == b)
return a;
if (a == 0)
return b;
if (b == 0)
return a;
if ((~a & 1) == 1)
{
if ((b & 1) == 1)
return gcd(a >> 1, b);
else
return gcd(a >> 1, b >> 1) << 1;
}
if ((~b & 1) == 1)
return gcd(a, b >> 1);
if (a > b)
return gcd((a - b) >> 1, b);
return gcd((b - a) >> 1, a);
}
let a = 34, b = 17;
document.write( "Gcd of given "
+ "numbers is " + gcd(a, b));
</script>
|
Output
Gcd of given numbers is 17
Time Complexity: O(N*N) where N is the number of bits in the larger number.
Auxiliary Space: O(N*N) where N is the number of bits in the larger number.
You may also like – Basic and Extended Euclidean Algorithm
Advantages over Euclid’s GCD Algorithm
- Stein’s algorithm is optimized version of Euclid’s GCD Algorithm.
- it is more efficient by using the bitwise shift operator.
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...