Greatest common divisor problem description
Write a program that asks for a greatest common divisor of two positive integers. If two positive integers are large, what are the simple algorithms?
Analysis and solution of "solution one"
The simplest implementation is to use code directly to realize the method of the division of the Euclidean. From the above description, we know that it is easy to get this problem done with recursion.
The specific code is as follows:
1 PackageCHAPTER2SHUZIZHIMEI.GCD;2 /**3 * Greatest common divisor problem4 * "Solution one" and "divide" method5 * @authorDELL6 *7 */8 Public classGCD {9 //Beg Greatest common divisorTen Public Static intgcdintXinty) { One return(y==0)? X:GCD (y,x%y); A } - Public Static voidMain (string[] args) { - intx, y; thex = 42; -y = 30; -The greatest common divisor of System.out.println (x+ "and" +y+ ") are:" +gcd (x, y)); - } + -}
The results of the program run as follows:
The greatest common divisor of 42 and 30 are: 6
"Solution Two"
In the solution one, the modulo operation is used. For large integers, modulo operations (where division is used) are very expensive and will become a bottleneck for the entire algorithm. The number of conventions for x and Y is the same as the number of conventions for Y and Y, so we can use subtraction. The code is as follows:
1 PackageCHAPTER2SHUZIZHIMEI.GCD;2 /**3 * Greatest common divisor problem4 * "solution two" recursive calculation, without division, with subtraction5 * @authorDELL6 *7 */8 Public classGCD2 {9 //Beg Greatest common divisorTen Public Static intgcdintXinty) { One if(x<y) A returngcd (y,x); - if(y==0) - returnx; the Else - returnGCD (y,x-y); - } - Public Static voidMain (string[] args) { + intx, y; -x = 42; +y = 30; AThe greatest common divisor of System.out.println (x+ "and" +y+ ") are:" +gcd (x, y)); at } - -}
The results of the program run as follows:
The greatest common divisor of 42 and 30 are: 6
This algorithm avoids the tedious of large integer division, but there are also shortcomings. The biggest bottleneck is the number of iterations is more than the previous algorithm, if you encounter (10 000 000 000 000,1) such a situation, it will be quite depressing.
"Solution three"
Combined with the solution of a two, the analysis is as follows:
1 PackageCHAPTER2SHUZIZHIMEI.GCD;2 /**3 * Greatest common divisor problem4 * "solution three" solution--the combination of the two methods of reconciliation5 * @authorDELL6 *7 */8 Public classGCD3 {9 Public Static BooleanIsEven (intx) {Ten if((X&0X01) ==1) One return false; A Else - return true; - } the //Beg Greatest common divisor - Public Static intgcdintXinty) { - if(x<y) - returngcd (y,x); + if(y==0) - returnx; + Else{ A if(IsEven (x)) { at if(IsEven (y)) - return2*GCD (x>>1,y>>1); - Else - returnGCD (x>>1, y); -}Else{ - if(IsEven (y)) in returnGCD (x,y>>1); - Else to returnGCD (y,x-y); + } - } the } * Public Static voidMain (string[] args) { $ intx, y;Panax Notoginsengx = 42; -y = 30; theThe greatest common divisor of System.out.println (x+ "and" +y+ ") are:" +gcd (x, y)); + } A the}
The results of the program run as follows:
The greatest common divisor of 42 and 30 are: 6
The 2nd chapter The charm of numbers--greatest common divisor problem