#include<stdio.h>
int main()
{
int a,b;
while(scanf("%d%d",&a,&b)!=EOF)
{
printf("%d\n",gcd(a,b));
printf("%d\n",gcd1(a,b));
printf("%d\n",gcd2(a,b));
printf("%d\n",gcd3(a,b));
printf("%d\n",lcm(a,b));
printf("%d\n",lcm1(a,b));
}
}
/* 直观算法,逐个试商枚举*/
int gcd(int a,int b)
{
int i,t=1;
int c=(a<=b)? a:b;
for(i=2;i<=c;i++)
if(a%i==0 && b%i==0)
t=i;
return t;
}
/* 模拟短除法*/
int gcd1(int a,int b)
{
int i,t=1;
int c=(a<=b)? a:b;
for(i=2;i<=c;i++)
{
while(a%i==0 && b%i==0)
{
a=a/i;
b=b/i;
t*=i;
c=(a<=b)? a:b;
}
}
return t;
}
/* 根据欧几里德gcd(a,b)=gcd(b,a%b),辗转相除*/
int gcd2(int a,int b)
{
int i,t=1,temp,c;
if(a<b)
{
temp=a;
a=b;
b=temp;
}
while(b)
{
c=a;
a=b;
b=c%b;
}
return a;
}
int gcd3(int a,int b)
{
int c;
c=a%b;
while(c)
{
a=b;
b=c;
c=a%b;
}
return b;
}
int lcm(int a,int b)
{
return (a*b)/gcd2(a,b);
}
/* 模拟短除法*/
int lcm1(int a,int b)
{
int i,t=1,flag;
int c=(a>=b)? a:b;
for(i=2;i<=c;i++)
{
flag=1;
while(flag)
{
flag=0;
if(a%i==0)
{
a=a/i;
flag=1;
}
if(b%i==0)
{
b=b/i;
flag=1;
}
if(flag)
t*=i;
}
c=(a>=b)? a:b;
}
return t;
}
|