Ejemplo n.º 1
0
IntType gcd( const IntType & m, const IntType & n )
{
    if( m > 0 )
        return gcd1( n, m );
    else
        return gcd1( n, -m );
}
Ejemplo n.º 2
0
/* integers to compute the GCD of */
static long gcd1(long p, long q) {
	if(q > p) return gcd1(q,p);
	if(q == 0) return p;

	printf(" gcd(%ld,%ld) &=& gcd(%ld \\mod %ld, %ld) = gcd(%ld,%ld) \n",
	  p, q, p, q, q, q, p%q);
	return( gcd1(q, p % q) );
}
Ejemplo n.º 3
0
int main() {
	int a, b;
	printf("Enter two number: \n");
	while (scanf("%d%d", &a, &b) == 2) {
		if(a == 0 || b == 0) 
			break;
		printf("gcd1: %d\n", gcd1(a, b));
		printf("gcd2: %d\n", gcd1(a, b));
		printf("gcd3: %d\n", gcd1(a, b));
	}
	return 0;
}
Ejemplo n.º 4
0
/*********************************************
 * ch5, p106, 5. 输入两个正整数m和n,求其最大公约数和最小公倍数 
 *********************************************/ 
void ch5_5()
{
    printf("====ch5_5()\n");
	int m,n,gcd;
    printf("请输入两个正整数,求其最大公约数和最小公倍数。\n");
    scanf("%d%d",&m,&n);
    gcd = gcd1(m,n); 
    printf("%d,%d的最大公约数=%d,",m,n,gcd);
    printf("最小公倍数=%d\n",m*n/gcd); 
    /******************************************** 
    以下为各种方法的验证 
    // 循环语句的辗除法,求两个整数的最大公约数
	printf("319,377最大公约数:%d\n",gcd1(319,377)); //29

	// 循环语句的辗除法,求两个整数的最大公约数
	printf("319,377最大公约数:%d\n",gcd2(319,377)); //29

	// 递归调用的辗除法,求两个整数的最大公约数
	printf("377,319最大公约数:%d\n",gcd3(377,319)); //29
	printf("319,377最大公约数:%d\n",gcd3(319,377)); //29
	printf("377,319最大公约数:%d\n",gcd4(377,319)); //29
	printf("319,377最大公约数:%d\n",gcd4(319,377)); //29
	printf("377,319最大公约数:%d\n",gcd5(377,319)); //29
	printf("319,377最大公约数:%d\n",gcd5(319,377)); //29

	// 最小公倍数=两整数的乘积÷最大公约数
	printf("319,377最小公倍数:%d\n",multiple (319,377)); 
    **********************************************/ 
}
Ejemplo n.º 5
0
 long gcd(long m)
 {
     long i=2;
     while(gcd1(i,m)!=1)
         i++;
     return i;
 }
Ejemplo n.º 6
0
// n is guaranteed non-negative
IntType gcd1( const IntType & n, const IntType & m )
{
    if( n % m == 0 )
        return m;
    else
        return gcd1( m, n % m );
}
Ejemplo n.º 7
0
int main(void) {
  long p, q;
  long x, y, g1, g2;

  while(scanf("%ld %ld", &p, &q)!=EOF) {
    printf("gcd of p = %ld and q = %ld = %ld\n",p, q, g1 = gcd1(p, q));
    printf(" %ld*%ld + %ld*%ld = %ld\n",p, x, q, y, g2 = gcd(p, q, &x, &y));

    if(g1 != g2) printf("ERROR: GCD\n");
    if((p*x + q*y) != g1) printf("ERROR: DIOPHONINE SOLUTION WRONG!\n");
  }
  return 0;      
}
Ejemplo n.º 8
0
int main(void)
{
    int gcd, lcm, m, n;  
    int repeat, ri;  

    scanf("%d", &repeat);
    for(ri = 1; ri <= repeat; ri++){
        scanf("%d", &m);
        scanf("%d", &n);
        if(m <= 0 || n <= 0)
            printf("m <= 0 or n <= 0");
        else{
/*---------*/
            gcd=gcd1(m,n);
            lcm=m/gcd*n; 
            printf("%d is the least common multiple of %d and %d, %d is the greatest common divisor of %d and %d.\n", lcm, m, n, gcd, m, n);
        }
    }
}
Ejemplo n.º 9
0
void ch1()
{
	// 循环语句的辗除法,求两个整数的最大公约数
	printf("319,377最大公约数:%d\n",gcd1(319,377)); //29

	// 循环语句的辗除法,求两个整数的最大公约数
	printf("319,377最大公约数:%d\n",gcd2(319,377)); //29

	// 递归调用的辗除法,求两个整数的最大公约数
	printf("377,319最大公约数:%d\n",gcd3(377,319)); //29
	printf("319,377最大公约数:%d\n",gcd3(319,377)); //29
	printf("377,319最大公约数:%d\n",gcd4(377,319)); //29
	printf("319,377最大公约数:%d\n",gcd4(319,377)); //29
	printf("377,319最大公约数:%d\n",gcd5(377,319)); //29
	printf("319,377最大公约数:%d\n",gcd5(319,377)); //29

	// 最小公倍数=两整数的乘积÷最大公约数
	//printf("319,377最小公倍数:%d\n",multiple (319,377));  
}
Ejemplo n.º 10
0
int lcm(int a, int b)
{
	if (0 == b)return 0;
	return a*gcd1(a, b) / b;
}
Ejemplo n.º 11
0
int gcd1(int a, int b)
{
	return b == 0 ? 0 : gcd1(b, a % b);
}
Ejemplo n.º 12
0
inline bigInt gcd(const bigInt& a, const bigInt& b)
{
    return gcd1(abs(a), abs(b));
}
Ejemplo n.º 13
0
inline bigInt gcd1(const bigInt& a, const bigInt& b)
{
    if (a == 0)
        return b;
    return gcd1(b % a, a);
}
Ejemplo n.º 14
0
// 最小公倍数=两整数的乘积/最大公约数
int multiple (int a,int b)  
{
	printf("最小公倍数=两整数的乘积÷最大公约数\n");
	return(a*b/gcd1(a,b));
}
Ejemplo n.º 15
0
int gcd1(int a,int b)
{
    if (b==0) return a;
    else return gcd1(b,a%b);
}