给定两个正整数M和N,任务是使用中学程序找到最大公约数(GCD)。
注意:两个整数的GCD是将两个整数相除的最大正整数。
例子:
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
使用中学过程GCD(m,n)查找GCD的算法:
- 求m的素因式分解。
- 求n的素因式分解。
- 找到所有常见的主要因素。
- 计算所有公质因数的乘积,并将其返回为gcd(m,n)。
下面是上述算法的实现:
C++
// C++ implementation of above algorithm
#include
#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);
}
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
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
输出:
Prime factor of 10 = 1*2*5
Prime factor of 15 = 1*3*5
GCD(10, 15) = 5