Given two positive integers M and N, the task is to find the greatest common divisor (GCD) using Middle School Procedure.
Note: GCD of two integers is the largest positive integer that divides both of the integers.
Input: m = 12, n = 14 Output: 2 Prime factor of 12 = 1*2*2*3 Prime factor of 14 = 1*2*7 GCD(12, 14) = 2 Input: m = 5, n = 10 Output: 5 Prime factor of 10 = 1*2*5 Prime factor of 5 = 1*5 GCD(5, 10) = 5
The algorithm to find GCD using Middle School procedure GCD(m, n):
- Find the prime factorization of m.
- Find the prime factorization of n.
- Find all the common prime factors.
- Compute the product of all the common prime factors and return it as gcd(m, n).
Below is the implementation of above algorithm:
Prime factor of 10 = 1*2*5 Prime factor of 15 = 1*3*5 GCD(10, 15) = 5
- Program to find GCD or HCF of two numbers
- Program to find LCM of two numbers
- Program to find LCM of 2 numbers without using GCD
- Program to find first N Fermat Numbers
- Program to find sum of first n natural numbers
- Program to find sum of prime numbers between 1 to n
- Program to find LCM of two Fibonnaci Numbers
- Program to find first N Iccanobif Numbers
- Program to find GCD of floating point numbers
- Program to find the percentage of difference between two numbers
- Program to find the common ratio of three numbers
- C program to Find the Largest Number Among Three Numbers
- Program to find the maximum difference between the index of any two different numbers
- Program to find HCF (Highest Common Factor) of 2 Numbers
- Program to find count of numbers having odd number of divisors in given range
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.