Program to find GCD or HCF of two numbers using Middle School Procedure
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.
Examples:
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:
C++
// C++ implementation of above algorithm #include <bits/stdc++.h> #define MAXFACTORS 1024 using namespace std; // struct to store factorization of m and n typedef struct{ int size; int factor[MAXFACTORS + 1]; int exponent[MAXFACTORS + 1]; } FACTORIZATION; // Function to find the factorization of M and N void FindFactorization(int x, FACTORIZATION* factorization) { int i, j = 1; int n = x, c = 0; int k = 1; factorization->factor[0] = 1; factorization->exponent[0] = 1; for (i = 2; i <= n; i++) { c = 0; while (n % i == 0) { c++; // factorization->factor[j]=i; n = n / i; // j++; } if (c > 0) { factorization->exponent[k] = c; factorization->factor[k] = i; k++; } } factorization->size = k - 1; } // Function to print the factors void DisplayFactorization(int x, FACTORIZATION factorization) { int i; cout << "Prime factor of << x << = "; for (i = 0; i <= factorization.size; i++) { cout << factorization.factor[i]; if (factorization.exponent[i] > 1) cout << "^" << factorization.exponent[i]; if (i < factorization.size) cout << "*"; else cout << "\n"; } } // function to find the gcd using Middle School procedure int gcdMiddleSchoolProcedure(int m, int n) { FACTORIZATION mFactorization, nFactorization; int r, mi, ni, i, k, x = 1, j; // Step 1. FindFactorization(m, &mFactorization;); DisplayFactorization(m, mFactorization); // Step 2. FindFactorization(n, &nFactorization;); DisplayFactorization(n, nFactorization); // Steps 3 and 4. // Procedure algorithm for computing the // greatest common divisor. int min; i = 1; j = 1; while (i <= mFactorization.size && j <= nFactorization.size) { if (mFactorization.factor[i] < nFactorization.factor[j]) i++; else if (nFactorization.factor[j] < mFactorization.factor[i]) j++; else /* if arr1[i] == arr2[j] */ { min = mFactorization.exponent[i] > nFactorization.exponent[j] ? nFactorization.exponent[j] : mFactorization.exponent[i]; x = x * mFactorization.factor[i] * min; i++; j++; } } return x; } // Driver code int main() { int m = 10, n = 15; cout << "GCD(" << m << ", " << n << ") = " << gcdMiddleSchoolProcedure(m, n); return (0); } |
chevron_right
filter_none
Java
// Java implementation of above algorithm class GFG { static final int MAXFACTORS = 1024 ; // class to store factorization // of m and n static class FACTORIZATION { int size; int factor[] = new int[MAXFACTORS + 1]; int exponent[] = new int[MAXFACTORS + 1]; } // Function to find the // factorization of M and N static void FindFactorization(int x, FACTORIZATION factorization) { int i, j = 1; int n = x, c = 0; int k = 1; factorization.factor[0] = 1; factorization.exponent[0] = 1; for (i = 2; i <= n; i++) { c = 0; while (n % i == 0) { c++; // factorization.factor[j]=i; n = n / i; // j++; } if (c > 0) { factorization.exponent[k] = c; factorization.factor[k] = i; k++; } } factorization.size = k - 1; } // Function to print the factors static void DisplayFactorization(int x, FACTORIZATION factorization) { int i; System.out.print("Prime factor of " + x + " = "); for (i = 0; i <= factorization.size; i++) { System.out.print(factorization.factor[i]); if (factorization.exponent[i] > 1) System.out.print( "^" + factorization.exponent[i]); if (i < factorization.size) System.out.print("*"); else System.out.println( ); } } // function to find the gcd // using Middle School procedure static int gcdMiddleSchoolProcedure(int m, int n) { FACTORIZATION mFactorization = new FACTORIZATION(); FACTORIZATION nFactorization = new FACTORIZATION(); int r, mi, ni, i, k, x = 1, j; // Step 1. FindFactorization(m, mFactorization); DisplayFactorization(m, mFactorization); // Step 2. FindFactorization(n, nFactorization); DisplayFactorization(n, nFactorization); // Steps 3 and 4. // Procedure algorithm for computing the // greatest common divisor. int min; i = 1; j = 1; while (i <= mFactorization.size && j <= nFactorization.size) { if (mFactorization.factor[i] < nFactorization.factor[j]) i++; else if (nFactorization.factor[j] < mFactorization.factor[i]) j++; else /* if arr1[i] == arr2[j] */ { min = mFactorization.exponent[i] > nFactorization.exponent[j] ? nFactorization.exponent[j] : mFactorization.exponent[i]; x = x * mFactorization.factor[i] * min; i++; j++; } } return x; } // Driver code public static void main(String args[]) { int m = 10, n = 15; System.out.print("GCD(" + m + ", " + n + ") = " + gcdMiddleSchoolProcedure(m, n)); } } // This code is contributed by Arnab Kundu |
chevron_right
filter_none
C#
// C# implementation of above algorithm using System; public class GFG { static readonly int MAXFACTORS = 1024 ; // class to store factorization // of m and n public class FACTORIZATION { public int size; public int []factor = new int[MAXFACTORS + 1]; public int []exponent = new int[MAXFACTORS + 1]; } // Function to find the // factorization of M and N static void FindFactorization(int x, FACTORIZATION factorization) { int i; int n = x, c = 0; int k = 1; factorization.factor[0] = 1; factorization.exponent[0] = 1; for (i = 2; i <= n; i++) { c = 0; while (n % i == 0) { c++; // factorization.factor[j]=i; n = n / i; // j++; } if (c > 0) { factorization.exponent[k] = c; factorization.factor[k] = i; k++; } } factorization.size = k - 1; } // Function to print the factors static void DisplayFactorization(int x, FACTORIZATION factorization) { int i; Console.Write("Prime factor of " + x + " = "); for (i = 0; i <= factorization.size; i++) { Console.Write(factorization.factor[i]); if (factorization.exponent[i] > 1) Console.Write( "^" + factorization.exponent[i]); if (i < factorization.size) Console.Write("*"); else Console.WriteLine(); } } // function to find the gcd // using Middle School procedure static int gcdMiddleSchoolProcedure(int m, int n) { FACTORIZATION mFactorization = new FACTORIZATION(); FACTORIZATION nFactorization = new FACTORIZATION(); int i, x = 1, j; // Step 1. FindFactorization(m, mFactorization); DisplayFactorization(m, mFactorization); // Step 2. FindFactorization(n, nFactorization); DisplayFactorization(n, nFactorization); // Steps 3 and 4. // Procedure algorithm for computing the // greatest common divisor. int min; i = 1; j = 1; while (i <= mFactorization.size && j <= nFactorization.size) { if (mFactorization.factor[i] < nFactorization.factor[j]) i++; else if (nFactorization.factor[j] < mFactorization.factor[i]) j++; else /* if arr1[i] == arr2[j] */ { min = mFactorization.exponent[i] > nFactorization.exponent[j] ? nFactorization.exponent[j] : mFactorization.exponent[i]; x = x * mFactorization.factor[i] * min; i++; j++; } } return x; } // Driver code public static void Main(String []args) { int m = 10, n = 15; Console.Write("GCD(" + m + ", " + n + ") = " + gcdMiddleSchoolProcedure(m, n)); } } // This code contribut by Rajput-Ji |
chevron_right
filter_none
Output:
Prime factor of 10 = 1*2*5 Prime factor of 15 = 1*3*5 GCD(10, 15) = 5
Recommended Posts:
- 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 contribute@geeksforgeeks.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.


