Pseudocode for GCD of two numbers in Python

GCD (Greatest Common Divisor) or HCF (Highest Common Factor) of two numbers is the largest number that divides both of them. 

For example GCD of 20 and 28 is 4 and GCD of 98 and 56 is 14. For solution  suppose a=98 & b=56  

a>b so put a= a-b and b is  remain same  so  a=98-56=42  & b= 56 . Now b>a  so  b=b-a and  a is same 

b= 56-42 = 14 & a= 42   . 42 is  3 times of 14  so HCF is 14  . likewise  a=36  & b=60  ,here b>a  so b = 24 & a= 36  now a>b so a= 12 & b= 24  . 12 is HCF of 36 and 60 .  This  concept  is  always  satisfying. 



Simple Solution :

Approach : For finding GCD of two numbers we will  first find the minimum of the two numbers and then find the  highest common factor of that  minimum which is also the factor of the other number.

        if(a%result==0 && b%result==0)

    cout<<"GCD of "<<a<<" and "<<b<<" is "<<gcd(a, b);

        if(a%result==0 && b%result==0)

    printf("GCD of %d and %d is %d ", a, b, gcd(a, b));

Output GCD of 98 and 56 is 14

Time Complexity : O(min(a,b)) 

Auxiliary Space: O(1)  or constant

An efficient solution is to use Euclidean algorithm which is the main algorithm used for this purpose. The idea is, GCD of two numbers doesn’t change if smaller number is subtracted from a bigger number. 

    cout<<"GCD of "<<a<<" and "<<b<<" is "<<gcd(a, b);

    printf("GCD of %d and %d is %d ", a, b, gcd(a, b));

    static int gcd(int a, int b)

    public static void main(String[] args)

        System.out.println("GCD of " + a +" and " + b + " is " + gcd(a, b));

    print('GCD of', a, 'and', b, 'is', gcd(a, b))

    static int gcd(int a, int b)

    public static void Main()

        Console.WriteLine("GCD of "

          + a +" and " + b + " is "

        return gcd( $a-$b , $b ) ;

    return gcd( $a , $b-$a ) ;

echo "GCD of $a and $b is ", gcd($a , $b) ;

    document.write("GCD of "+ a + " and " + b + " is " + gcd(a, b));

Output GCD of 98 and 56 is 14

Time Complexity: O(min(a,b))

Auxiliary Space: O(min(a,b))

Dynamic Programming Approach (Top Down Using Memoization) :

int static dp[1001][1001];

    memset(dp, -1, sizeof(dp));

    cout<<"GCD of "<<a<<" and "<<b<<" is "<<gcd(a, b);

    static int [][]dp = new int[1001][1001];

    static int gcd(int a, int b)

    public static void main(String[] args)

        for(int i = 0; i < 1001; i++) {

            for(int j = 0; j < 1001; j++) {

        System.out.println("GCD of " + a +" and " + b + " is " + gcd(a, b));

dp = [[-1 for i in range(1001)] for j in range(1001)]

    print('GCD of', a, 'and', b, 'is', gcd(a, b))

    static int [,]dp = new int[1001, 1001];

    static int gcd(int a, int b)

    public static void Main()

        for(int i = 0; i < 1001; i++) {

            for(int j = 0; j < 1001; j++) {

        Console.Write("GCD of " + a +" and " + b + " is " + gcd(a, b));

var dp = new Array(1001);

for (var i = 0; i < dp.length; i++) {

    for(let i = 0; i < 1001; i++) {

        for(let j = 0; j < 1001; j++) {

    document.write("GCD of "+ a + " and " + b + " is " + gcd(a, b));

Output GCD of 98 and 56 is 14

Time Complexity: O(min(a,b))

Auxiliary Space: O(1)

A more efficient solution is to use modulo operator in Euclidean algorithm.

    return b == 0 ? a : gcd(b, a % b);   

    cout<<"GCD of "<<a<<" and "<<b<<" is "<<gcd(a, b);

    printf("GCD of %d and %d is %d ", a, b, gcd(a, b));

    static int gcd(int a, int b)

    public static void main(String[] args)

        System.out.println("GCD of " + a +" and " + b + " is " + gcd(a, b));

    print('GCD of', a, 'and', b, 'is', gcd(a, b))

    static int gcd(int a, int b)

    public static void Main()

        Console.WriteLine("GCD of "

          + a +" and " + b + " is "

    return gcd( $b , $a % $b ) ;

echo "GCD of $a and $b is ", gcd($a , $b) ;

document.write(`GCD of ${a} and ${b} is ${gcd(a, b)}`);

Output GCD of 98 and 56 is 14

Time Complexity: O(log(min(a,b))

Auxiliary Space: O(log(min(a,b))

The time complexity for the above algorithm is O(log(min(a,b))) the derivation for this is obtained from the analysis of the worst-case scenario. What we do is we ask what are the 2 least numbers that take 1 step, those would be (1,1). If we want to increase the number of steps to 2 while keeping the numbers as low as possible as we can take the numbers to be (1,2). Similarly, for 3 steps, the numbers would be (2,3), 4 would be (3,5), 5 would be (5,8). So we can notice a pattern here, for the nth step the numbers would be (fib(n),fib(n+1)).  So the worst-case time complexity would be O(n) where a>= fib(n) and b>= fib(n+1). 

Now Fibonacci series is an exponentially growing series where the ratio of nth/(n-1)th term approaches (sqrt(5)+1)/2 which is also called the golden ratio. So we can see that the time complexity of the algorithm increases linearly as the terms grow exponentially hence the time complexity would be log(min(a,b)).

Please refer GCD of more than two (or array) numbers to find HCF of more than two numbers.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above


Article Tags :

Practice Tags :