Maximum common approx. Least Common multiple

Source: Internet
Author: User

Calculate the maximum common approx.

There are two methods: Euclidean Algorithm and Stein algorithm. Euclidean algorithms are classified into general and Extended Euclidean algorithms.

The minimum public multiples of the two numbers are used to calculate their maximum public approx. The formula is lcm (a, B) = a * B/gcd (A, B ). LCM is the abbreviation of least public multiple, and GCD is the same.

Therefore, the code for solving the least common multiple is described only in the analysis of Euclidean algorithms.

 

I. Euclidean Algorithm

1. General Euclidean Algorithm (only calculate the maximum common approx)

A classic method for finding the maximum common divisor is the division of the moving phase. The code to be introduced below is also solved using it.

Recursive and non-recursive methods are used to solve the maximum common approx. comments are provided in the Code.

 

The Code is as follows:

# Include <iostream> using namespace STD; int gcd (int A, int B) // A, the maximum approximate number of B, recursion {If (B = 0) return; else return gcd (B, A % B);} int gcd (int A, int B) // A, B maximum common approx. Non-recursion {int TEM; if (A <B) {TEM = A; A = B; B = TEM;} int REM = 1; while (REM! = 0) {REM = A % B; A = B; B = REM;} return a;} int lcm (int A, int B) // minimum public multiple {return a * B/gcd (a, B);} int main () {int A, B, LC, gc1, GC2; cout <"input: "; while (CIN> A> B) {gc1 = gcd (a, B); GC2 = gcd (a, B); lc = lcm (A, B ); cout <"maximum public approx.:" <gc1 <Endl; cout <"maximum public approx.:" <GC2 <Endl; cout <"minimum public multiple: "<LC <Endl; cout <" input: ";} return 0 ;}

Test results:

Let's say a few more. It should be well understood that the non-Recursive Method for Solving the maximum common divisor is a complete division of the moving phase. The recursive method is confusing because it does not determine the size relationship between A and B.

For example, when the input is 28 1274, some people may feel that the problem cannot be solved. In fact, input 28 1274 is basically the same as input 1274 28. Explanation: Enter 28 1274 and call gcd ),

B = 1274, B! = 0, so execute return gcd (B, A % B), that is, return gcd ). Because 28% 1274 = 28! At this point, it is the same as directly entering 1274 28.

2. Extended Euclidean Algorithm (not only can we find the maximum common divisor, but also can solve the X and Y in the equation AX + by = gcd (a, B)

For details about the description and proof, see: Click to open the link.

 

Algorithm code:

Int extendgcd (int A, int B, Int & X, Int & Y) // extended GCD, you can find gcd (A, B) and AX + by = gcd (, b) The value of x and y in {If (B = 0) {x = 1; y = 0; return a;} else {int a1 = extendgcd (B, A % B, x, y); int TMP = x; X = y; y = TMP-A/B * Y; return A1 ;}}

 

Ii. Stein Algorithm

The difference between the Stein algorithm and the Euclidean algorithm is that the Stein algorithm only involves integer shift and addition and subtraction, and division and modulo operation are not required.

 

Algorithm Description:

(1) judge whether A or B is 0. If a = 0, B is the smallest public multiple. If B = 0, A is the smallest public multiple. The algorithm ends.

(2) Set a1 = A, B1 = B, C1 = 1.

(3) judge whether an and bn are even

① If they are all even numbers, a (n + 1) = An/2, B (n + 1) = Bn/2, C (n + 1) = 2 * CN.

② If an is an even number, a (n + 1) = An/2, B (n + 1) = BN, C (n + 1) = cn.

③ If BN is an even number, a (n + 1) = An, B (n + 1) = Bn/2, C (n + 1) = cn.

④ If an and bn are odd, a (n + 1) = | an-Bn |, B (n + 1) = min (an, bn ), C (n + 1) = cn.

(4) n accumulates 1 and jumps to (3) for the next round of calculation.

 

The algorithm code is as follows:

Method 1:

Int gcd (int A, int B) {int min = (A> B )? B: A; int max = (A> B )? A: B; If (min = 0) return Max; If (max % 2 = 0 & min % 2 = 0) return 2 * gcd (max/2, MIN/2); If (max % 2 = 0) return gcd (max/2, min); If (Min % 2 = 0) return gcd (max, MIN/2); Return gcd (MAX + min)/2, (max-min)/2); // All values are odd, which seems to be different from the algorithm, I tested several groups of data that are correct ...}

Method 2:

Int gcd (int A, int B) {int min = (A> B )? B: A; int max = (A> B )? A: B; If (! Min) return Max; If (! Max) return min; If (! (Min & 1 )&&! (Max & 1) // return gcd (min> 1, max> 1) <1; if (! (Min & 1) // return gcd (min> 1, max); If (! (Max & 1) // return gcd (Min, max> 1); // return gcd (max-min)> 1, min ); // they are all odd. I don't know why ??? Return gcd (max-min, min); // All values are odd}

 

 

 

 

 

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.