最大公约数的三种算法

#include <stdio.h>
#include <math.h>
#include <iostream.h>
#include <string.h>

#define ProdNum   20

#define MIN(x, y) ((x) <= (y) ? x:y)

//these two arrays are to save the product of the two number to be divided
int Product1[ProdNum];
int Product2[ProdNum];
//these array to save the result
int Product3[ProdNum];

/************************************************************************/
/* this function is to implement the algorithm by gcd         */
/************************************************************************/
int Function1(int m,int n)
{
 int r = 0; //the residue value
 int result = 0;//the variable to save the result
 if (!n)
 {
  return m;
 }
 else
 {
  r = m%n;
  m = n;
  n = r;
  result = Function1(m,n);
 }

 return result;
}

/************************************************************************/
/* this function is to  implement the algorithm continuous      */
/************************************************************************/
int Function2(int m, int n)
{
 int t = 0;
 t = MIN(m,n);
 while (t)
 {
  if ((m%t) || (n%t))
  {
   t--;
  }
  else
   return t;
 }

 return 1;
}

/************************************************************************/
/* this series functions are to implement the algorithm mid school          */
/************************************************************************/

//this function is to decide whether the number is a prime number
bool IsPrimeNum(int Number)
{
 int temp = 0;
 bool IsPri = true;
 temp  = (int)(sqrt((double)Number));
 for (int i=temp; i>1; i--)
 {
  if (!(Number%i))
  {
   IsPri = false;
   break;
  }
 }

 return IsPri;
}

//this function is to divide the number to the product of prime numbers
int pronum = 0;

int NumDivide(int Number, int *Product)
{
 int temp = 0;
 //static int pronum;
 int residual = 0;

 if (IsPrimeNum(Number))
 {
  cout << pronum << ":" << Number << endl;
  Product[pronum] = Number;
  pronum++;
 }
 else
 {
  temp = (int)(sqrt(double(Number)));
  for (int i=temp; i>1; i--)
  {
   residual = Number%i;
   if (!residual)
   {
    NumDivide(i,Product);
    Number = Number/i;
    NumDivide(Number,Product);
    break;
   }
  }
 }
 
 Product[pronum] = 1;
 return pronum;
}

//this function is to sort the number in one array
int SelectSort(int pronum, int *product)
{
 int temp1 = 0;
 for (int i=0; i<pronum-1; i++)
 {
  for (int j=i+1; j<pronum; j++)
  {
   if (product[i] > product[j])
   {
    temp1 = product[i];
    product[i] = product[j];
    product[j] = temp1;
   }
  }
 }

 return 1;
}

//this function is to find the same value in two arrays
int SameStat(int pronum1, int *pro1, int pronum2, int *pro2)
{
 int i=0,j=0,k=0;
 while (i<pronum1 && j<pronum2)
 {
  if (pro1[i] < pro2[j])
  {
   i++;
  }
  else if (pro1[i] > pro2[j])
  {
   j++;
  }
  else
  {
   Product3[k] = pro1[i];
   i++;
   j++;
   k++;
  }
 }

 return k;
}
int Function3(int m, int n)
{
 int divnum = 0;
 int pronumm=0,pronumn=0;
 int result = 1;

 memset(Product1,0,sizeof(Product1));
 memset(Product2,0,sizeof(Product2));
 pronumm = NumDivide(m,Product1);
 pronum = 0;
 pronumn = NumDivide(n,Product2);
 pronum = 0;

 SelectSort(pronumm,Product1);
 SelectSort(pronumn,Product2);

 divnum = SameStat(pronumm,Product1,pronumn,Product2);
 for (int i=0; i<divnum; i++)
 {
  cout << Product3[i] << "  ";
  result = result * Product3[i];
 }

 cout << endl;
 return result;
}


void main()
{
 int m=0,n=0,result=0;
 cout << "Please input the two variable:" << endl;
 cin >> m >> n;

 
 result = Function1(m,n);
 cout << "gcd function: " << result << endl;

 result = Function2(m,n);
 cout << "continuous function: " << result << endl;
 
 result = Function3(m,n);
 cout << "mid function: " << result << endl;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值