How to find the greatest common divisor of two numbers
One:
more subtractive loss
//Well-known variants of the Euclidean method
Main ()
{
int A, b;
scanf ("%d%d", &a,&b);
While (a!=b)
{
if (a>b)
a-=b;
Else
B-=a;
}
printf ("%d", a);
}
Two:
Divide
//General recursive description algorithm
gcd (m,n) is the same as GCD (n,m mod n) why?
gcd (m,0) =m why?
two numbers do not need to compare the size is ok why?
Main ()
{
int a,b,r,t;
scanf ("%d%d", &a,&b);
if (a<b)
{
T=a;
a=b;
b=t;
}
Do
{
r=a%b;
a=b;
b=r;
}while (r!=0);
printf ("%d", a);
}
Three:
Continuous integer Detection
//According to the definition of greatest common divisor
Main ()
{
int a,b,t;
scanf ("%d%d", &a,&b);
if (a<b)
T=a;
Else
t=b;
While (t>=1)
{if ((a%t==0) && (b%t==0))
Break ;
t--;
}
printf ("%d", t);
}
Four:
Short Division
//
two number of public factorization multiplied by P common factor the number of times MIN{PA,PB}
int primedec (int m,int *a)//Why can this function find all the factorization of M?
{
int i=0,k=2;//is divided by the smallest prime number 2
While (k<=m)
{
if (m%k==0)
{
a[i++]=k;
m=m/k;
}
Else
k++;
}
}
Main ()
{
int a[10]={0},b[10]={0},c[10];
int M,n,i,j,k=0,s=1;
scanf ("%d%d", &m,&n);
Primedec (m,a);
Primedec (n,b);
For (i=0;i<10;i++)
{
For (j=0;j<10;j++)
{
if (a[i]==b[j]&&b[j]!=0)
{
C[k++]=a[i];
b[j]=0;
Break ;
}
}
}
For (i=0;i<k;i++)
{
S*=c[i];
}
printf ("%d", s);
}
How to find the greatest common divisor of two numbers