The 2nd chapter The charm of numbers--greatest common divisor problem

Source: Internet
Author: User
Tags greatest common divisor integer division

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&AMP;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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.