


You are given ‘X’ as 20 and ‘Y’ as 15. The greatest common divisor, which divides both 15 and 20, is 5. Hence the answer is 5.
The first line of input contains ‘T’, representing the number of test cases.
The first line of each test case contains two space-separated integers, ‘X’ and ‘Y’, representing the given numbers.
For each test case, print a single integer representing the Greatest Common Divisor of ‘X’ and ‘Y’.
Print a separate line for each test case.
You do not need to print anything, it has already been taken care of. Just implement the given function.
1 <= T <= 10
1 <= X, Y <= 10^9
In this approach, we know the minimum value of a common factor of two integers is 1, and the maximum value of a common factor is the minimum value of the given two integers.
Therefore we will maintain a variable ans to store GCD. We set the initial value of ans as 1. We will iterate through the integers from 2 to the minimum of X and Y. We will check for each integer if X and Y are divisible by the integer, then update the ans with the integer. In the end, we return the variable ans.
Algorithm:
In this approach, we can use the Euclidian theorem that states the gcd of two numbers X and Y doesn’t change if we subtract the larger number from the smaller number. Therefore we can keep subtracting the smaller number from the larger number until the larger number is greater than, the smaller number.
Instead of repeatedly subtracting the number, we can use the modulo of the larger number with the smaller number and repeat the process until one of the numbers becomes 0, then we return the other number.
Therefore according to the Euclidian Theorem -:
gcd(X, Y) = gcd(Y, X % Y)
If X and Y are not equal 0.We will create a recursive function findGcd(X, Y) that returns the greatest common divisor of the numbers X and Y.
Algorithm:
Merge Two Sorted Arrays Without Extra Space
Merge Two Sorted Arrays Without Extra Space
Co-Prime
Count Repeating Digits
First Digit One
Special Digit Numbers