visit
For example: let's take two numbers 75 and 30 so the highest number that can divide both numbers will be 15.
Explanation: The numbers that can divide both 75 and 30 are [1,3,5,15] so the highest number among these is 15 so it will be the GCD of 75 and 30.
Example 2: let's take two numbers 128 and 32 so the highest number that can divide both numbers will be 32.
Explanation: The numbers that can divide both 128 and 32 are [1,2,4,8,16,32] so the highest number among these is 32 so it will be the GCD of 128 and 32.
Example:
Input Numbers: N1 = 132 & N2 = 24Smallest Number: N2 = 24We will start a loop from 24 that will end at 1. We will then find the first number that will completely divide both numbers N1 and N2, in this case, it is 12. So the GCD of N1 and N2 will be 12.C Program of the above algorithm
#include <stdio.h>
int main()
{
int num1, num2, i;
//Accepting and storing input of two numbers from user
printf("Enter two numbers: ");
scanf("%d ", &num1);
scanf("%d ", &num2);
int minNum=0;
int result = 1;
//Calculating smallest number among num1 and num2
if(num1<num2){
minNum = num1;
} else {
minNum = num2;
}
//loop from minNum to 1
for( i = minNum; i > 1; --i )
{
//check if both num1 and num2 is divisible by i or not
//store the outcome and break out of the loop
if(num1%i == 0 && num2%i == 0){
result = i;
break;
}
}
//Print the result
printf("GCD of %d and %d is %d", num1, num2, result);
return 0;
}
Input: 132, 24
Output: GCD of 132 and 24 is 12.
Explanation: The smallest number among 132 and 24 is 24 so the loop starts from 24 and checks if 132 and 24 are divisible by 24 or not since both are not divisible so the loop will continue to run and now it will check for 23 and so on until the loop reaches to 12 where both numbers will be divisible by 12 so it will store the result and break out of the loop and then print the result.
Time Complexity: Since the loop is running for the smallest number(n) to 1 so the time complexity will be O(n).
Space Complexity: It's not taking any extra space other than some variables which are unrelated to the input so the space complexity of this algorithm will be O(1).
We can reduce the time complexity of this algorithm by using an efficient approach.Example: Given two numbers: N1=132, N2=24.
See the following image for a better understanding
Algorithm of this Approach:
Define a function that takes two arguments divisor and dividend.Now in the function check if the divisor divides the dividend entirely or not. Return the divisor if it divides it entirely otherwise.Recursively invoke the function by supplying the divisor as the remainder of the divisor and dividend. In addition to the dividend as the current divisor.C program for the above algorithm
#include <stdio.h>
//Function to calculate GCD
int gcd(int divisor, int dividend)
{
//Calculating remainder
int remainder = dividend % divisor;
//Base condition
//If remainder is 0 then divisor will be GCD
if(remainder == 0)
{
return divisor;
}
//Recursive call to find GCD
return gcd(remainder, divisor);
}
int main()
{
int num1, num2;
//Accepting and storing input of two numbers from user
printf("Enter two numbers: ");
scanf("%d ", &num1);
scanf("%d ", &num2);
int dividend, divisor;
//dividend will be the number which is gretest among the input
if(num1 > num2){
dividend = num1;
} else {
dividend = num2;
}
//Divisor will be the number which is lowest among the input
if(num1 < num2){
divisor = num1;
} else {
divisor = num2;
}
//Calling function to calculate GCD
int ans = gcd(divisor, dividend);
//Print the result
printf("GCD of %d and %d is %d", num1, num2, ans);
return 0;
}
Input: 132, 24
Output: GCD of 132 and 24 is 12.
Time Complexity: O(log(min(num1,num2)) which is equivalent to O(log(n)).
Space Complexity: O(1), no extra space is used other than some variables.