Пример #1
0
// 递归调用的辗除法,求两个整数的最大公约数
int gcd3(int a,int b){
	printf("递归调用的辗除法,求两个整数的最大公约数gcd3(),a=%d,b=%d\n",a,b);
	// 不用比较a,b大小,如果b>a, a%b=a,第二次调用,自然将a,b调换,即大数a,小数b
	if (b==0) return a;
	else return gcd3(b,a%b);
	
}
Пример #2
0
int main () {
    printf("iterative sum of squares from 1 to 10: %d\n",
            summation(square, 1, 10));
    printf("recursive gcd of 24 and 40: %d\n", gcd(24, 40));
    printf("recursive sum of squares from 1 to 10: %d\n",
            summation2(square, 1, 10));
    printf("iterative gcd of 24 and 40: %d\n", gcd2(24, 40));
    printf("tail-recursive gcd of 24 and 40: %d\n", gcd3(24, 40));
}
Пример #3
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));  
}