From: Baidu Encyclopedia of the method of the Division of the divide: the greatest common divisor is a method of finding two natural numbers, also known as Euclidean algorithm.
For example, ask (319,377): ∵319÷377=0(remainder 319) ∴ (319,377=377,319); ∵377÷319=1(remainder 58) ∴ (377,319=319, -); ∵319÷ -=5(remainder 29) ∴ (319, -= -, in); ∵ -÷ in=2(remainder 0) ∴ ( -, in) = in; ∴ (319,377) = in。
By using the greatest common divisor of several numbers, we can find out the greatest common divisor of any two numbers, then ask for the greatest common divisor of the greatest common divisor and the third number, and then go down until the last number. The last greatest common divisor is the greatest common divisor of all these numbers.
More subtractive loss method: Also called more subtractive loss
The first step: arbitrarily given two positive integers, judging whether they are even. If so, use a 2 reduction, or the second step if not. The second step: reduce the smaller number by a larger number, and then compare the resulting difference with the smaller number, and subtract the decimal number by the large numbers. Continue this operation until the resulting meiosis and difference are equal. The product of several 2 and second intermediate numbers in the first step is the desired greatest common divisor. The "equal number", which is said, is greatest common divisor. The method of seeking "equal number" is "more subtractive". So the subtraction method is also called the equivalence algorithm.
Example 1. The greatest common divisor of 98 and 63 were obtained by using more subtractive lesions. Solution: Since 63 is not even, the number of 98 and 63 is reduced by a large number and subtracted:98- the= * the- *= - *- -=7 --7= + +-7= - --7=7So, the greatest common divisor of 98 and 63 equals 7. This process can be simply written as: (98, the= *, the= *, -=7, -=7, +=7, -=7,7) =7. Example 2. The greatest common divisor of 260 and 104 were obtained by using more subtractive lesions. Solution: Since both 260 and 104 are even, first 2 reduction is used to get 130 and 52, then 2 and 65. At this point 65 is odd and 26 is not odd, so subtract 65 and 26: $- -= the the- -= - -- -= -So, 260 and 104 greatest common divisor equals 13 times the first step of about two 2, which is*2*2= the. This process can be simply written as: (260,104)(/2/2) = ( $, -= the, -= -, -= -, -) = -. (*2*2) = the[1]
Comparison: Comparing the difference between the division method and the more subtractive loss (1) is the method to find the maximum common factor, the calculation of the divide-based method to divide the main, the more subtractive loss of the main subtraction, the calculation of the number of divided method is relatively small number of calculations, especially when the number of two different size difference is more obvious. (2) from the embodiment of the results, the result of the method of dividing the division is to divide the remainder of 0 is obtained, and the more subtractive loss is the same as the difference between meiosis and the code to achieve one: the simplest method
#include <stdio.h>#include<stdlib.h>intMain () {intm, n,temp,i; scanf ("%d", &m); scanf ("%d", &N); if(m>N) {temp=m; M=N; N=temp; } for(i = m; I >=1; i--) if(m%i==0&& n%i==0) Break; printf ("%d\n", i); System ("Pause"); return 0;}
Two: The method of division of the Divide (recursion)
#include <stdio.h>#include<stdlib.h>int gcd (int a, int b) {int mod; if (mod = a% b) = = 0) return b; return gcd (b, MoD);}intMain () {intm, N,ret; scanf ("%d", &m); scanf ("%d", &N); RET=gcd (M, n); printf ("%d", ret); System ("Pause"); return 0;}
Three: The method of dividing (non-recursive)
int gcd (intint b) { int mod=a% b; while (mod!=0) { = b; = mod; = a% b; } return b;}
Four: More subtractive method (non-recursive)
#include <stdio.h>#include<stdlib.h>#include<math.h>intgcdintAintb) { intVal= A-b; while(val! =b) {if(b>val) {a=b; b=Val; } Else{a=Val; } Val= A-b; } returnVal;}intMain () {intM, n,ret,temp,count=0; scanf ("%d", &m); scanf ("%d", &N); if(M = =N) {printf ("%d", M); return 0; } if(M <N) {temp=m; M=N; N=temp; } while(m%2==0&& n%2==0) {Count++; M/=2; N/=2; } ret=gcd (M, n); printf ("%d", ret* (int) Pow (2, Count))); The use of POW requires an (int) conversion, or it will error system ("Pause"); return 0;}
C Language Review---to obtain the maximum number of conventions (Euclidean method and subtract loss method)