Greatest common divisor of two numbers of the three methods and C language implementation

Zero, Overview

This paper describes the greatest common divisor of two numbers of the algorithm and its three C language implementation, the greatest common divisor defined herein need to know (or specific self encyclopedia or other way to understand).

Also herein, the C language used in Max_sort function is to ensure a necessarily than b, that if a <b, then it is exchanged, to facilitate subsequent encoding. code show as below:

int Max_sort(int *a, int *b)
{
	if (a == NULL || b == NULL) return -1;

	if (*a < *b)			//如果比较小则交换	
	{
		*a += *b;
		*b = *a - *b;
		*a -= *b;
	}
	return 0;
}

First, the algorithm body

1. Euclidean

As a flow chart, the value of the greatest common divisor of a and b of the request, the algorithm is simple: the division of a and b, and the remainder is assigned to a C, and determines whether 0, if not zero, then the assignment b to a, c will be assigned to a, and then repeat above steps until c is 0. a% b = c, the algorithm is the mathematical formula Intuitively left shift operation.

//辗转相除法
int ZZXC_Function(int a, int b)
{
	int c = 0;
	Max_sort(&a, &b);
	c = b;
	do
	{
		b = c;
		c = a%b;
		a = b;
	} while (c != 0);
	return b;
}

2. Method Decreases

As a flow chart, the value of the greatest common divisor of a and b of the request, the algorithm is simple: the reduction of a and b, the resulting difference is assigned to C, and determines whether or 0 if it is not 0, then the assignment b to a, c will be assigned to a, and then repeat above steps until c is 0. ab = c, the algorithm is this intuitive point of view of mathematical formula left shift, and the Euclidean algorithm implementation was very close, it should be the origin of the idea is the same.

//更相减损法
int GXJS_Function(int a, int b)
{
	int c = 0;
	Max_sort(&a, &b);
	c = b;
	do
	{
		b = c;
		a>b ? c = a - b : c = b - a;
		a = b;
	} while (c != 0);
	return b;
}

3. exhaustive method

As a flow chart, the value of the greatest common divisor of a and b of the request, the algorithm an exhaustive manner, simple and crude, simply is to compare a and b, the smaller the number assigned to c, c is less than or equal each value in turn and a and b modulo operation immediately if the remainder is 0, it means that the number of the greatest common divisor of a and b, if not, the value of c minus 1, repeat the above steps. As the code, when the value is relatively large, requires exhaustive number also increases, the efficiency decreased.

//穷举法 当数值比较大时,明显效率下降
int QJ_Function(int a, int b)
{
	int c;
	Max_sort(&a, &b);
	c = b;

	while (a%c != 0 || b%c != 0)
	{
		c--;
	}
	return c;
}

Second, the words

The greatest common divisor is feeling more mathematical problem using a mathematical model expressed by C language was obtained the above algorithm, in fact, there are various methods greatest common divisor prime factor decomposition and division, and short, but the computer languages implementing aspects of, herein three reproducible method is more suitable computer and realized. Therefore, the above algorithm can not only few, but also due to the implementation of different language and logic, it is important to expand this basic binary thinking to diverse thinking, solve the same problem in a more complex situation. Welcome to point out the fallacy or deficiencies, learn from each other -

Published 19 original articles · won praise 7 · views 6935

Guess you like

Origin blog.csdn.net/G_METHOD/article/details/86561334