Tips and Tricks to Find GCD Last Updated : 14 Oct, 2024 Comments Improve Suggest changes Like Article Like Report Let us first discuss the standard method to find GCD of two numbers a and b.Step 1: List all the divisors of the number ‘a’.Step 2: List all the divisors of the number ‘b’.Step 3: Identify the common divisors of both ‘a’ and ‘b’.Step 4: Select the largest number from the common divisors.GCD of Two Numbers : 12 and 18Divisors of 12 : 1, 2, 3, 4, 6 and 12Divisors of 18 : 1, 2, 3, 6, 9 and 18The common divisors are 1, 2, 3 and 6 and largest of these is 6 hence 6 is the GCDHow do we speed up our computations? Below are the tricks that you can use when manually computing GCD.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. 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 evenRepeat 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. Comment More infoAdvertise with us Next Article Tips and Tricks to Find GCD K kartik Follow Improve Article Tags : Mathematical School Learning DSA GCD-LCM Practice Tags : Mathematical Similar Reads Tips and Tricks to Check for Prime Checking for a large number whether it is prime or not can be difficult. This is so difficult that RSA algorithm uses this fact to encrypt data on Internet. However there are certain trick that we can use. The idea is reduce the number faster using divisibility rules. 1. If the given number has 0s a 2 min read Prime Factorization Tips and Tricks Let us first go through the standard Prime Factorization by the Division Method. Then we will be talking about tips and tricks to make it faster.Step 1: Divide the number by the smallest prime number (i.e. 2) until we are able to divide the given number without leaving any remainder.Step 2: Move on 2 min read Find any pair with given GCD and LCM Given gcd G and lcm L. The task is to print any pair which has gcd G and lcm L.Examples: Input: G = 3, L = 12 Output: 3, 12 Input: G = 1, L = 10 Output: 1, 10 A normal solution will be to perform iteration over all the factor pairs of g*l and check if any pair has gcd g and lcm as l. If they have, t 8 min read Find two numbers whose sum and GCD are given Given sum and gcd of two numbers a and b . The task is to find both the numbers a and b. If the numbers do not exist then print -1 .Examples: Input: sum = 6, gcd = 2 Output: a = 4, b = 2 4 + 2 = 6 and GCD(4, 2) = 2Input: sum = 7, gcd = 2 Output: -1 There are no such numbers whose sum is 7 and GCD is 5 min read GCD and Fibonacci Numbers You are given two positive numbers M and N. The task is to print greatest common divisor of M'th and N'th Fibonacci Numbers.The first few Fibonacci Numbers are 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, .... Note that 0 is considered as 0'th Fibonacci Number.Examples: Input : M = 3, N = 6 Output 9 min read Like