Algorithm--Greatest common divisor

Source: Internet
Author: User
Tags greatest common divisor

Baidu Encyclopedia on the introduction of the greatest common divisor (limit two number) There are two main: The method of the division and the more subtractive method.

Euclidean method

The Division method, Baidu Encyclopedia on the example:

The greatest common divisor of a and B are represented by (a, a).

For example, ask (319,377): ∵319÷377=0 (remainder 319) ∴ (319,377) = (377,319); ∵377÷319=1 (+ 58) ∴ (377,319) = (319,58); ∵319÷58=5 (+ 29), ∴ (319,58) = (58,29); ∵58÷29=2 (+ 0), ∴ (58,29) = 29;∴ (319,377) =29. That is, the two-digit greatest common divisor is preceded by the larger number divided by the smaller number, and if divisible, the greatest common divisor equals the smaller number, otherwise the smaller number is divided by the remainder of the first step. If divisible, the greatest common divisor equals the remainder of the first step, otherwise, the remainder of the previous step is divided by the current gain, until it can be divisible. At this point, the number that is the divisor is the greatest common divisor of the first two numbers. Here's the code:
1 functionGCM (m,n) {2    varA, C, E, F;3    //sort of m,n, smaller in front, larger in the rear4    if(M >N) {5A =m;6m =N;7n =A;8    }9c = m, E = n, f =C;Ten     while(c!== 0) { Onef = e%C; AE =C; -c =F; -    } the alert (e); -    returne; -}

The code is mostly a while loop that's a bit around. (Refer to the answer above)

The method of more subtractive loss

Baidu Encyclopedia on the explanation:

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 a number of 2 and second intermediate numbers that are approximately lost in the first step is the desired greatest common divisor. Example 1, using the more subtractive loss of 98 and 63 greatest common divisor. Solution: Since 63 is not even, the number of 98 and 63 is reduced by a large number and subtracted: 98-63=3563-35=2835-28=728-7=2121-7=1414-7=7 So, 98 and 63 of greatest common divisor equals 7. Example 2, the greatest common divisor of 260 and 104 were obtained with the more subtractive loss technique. 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 the 65 and 26 are subtracted: 65-26=3939-26=1326-13=13 so, 260 and 104 of greatest common divisor equals 13 times the first step of about two 2, namely 13*2*2=52. Here's the code:
1 functiongcm2 (m,n) {2    varA, C, E, F, index = 0;3    //sort of m,n, smaller in front, larger in the rear4    if(M >N) {5A =m;6m =N;7n =A;8    }9    //if the m,n are evenTen     while(m% 2 = = = 0 && n% 2 = = 0) { Onem = M/2; An = N/2; -index++; -    } the  -f = m, E = n, c = n-m; -     while(c!==f) { -E =F; +f =C; -c = e-F; +       if(C < 0) { Ac =-C; at       } -    } -Alert (f*2*index); -    returnf*2*index; -}

In the calculation, the division is mainly divided, and the subtraction method is mainly subtracted. The number of calculation times on the calculation times is relatively small, especially when the number of two digital size difference is more obvious than the number of calculations.

Ask for a number of greatest common divisor, you can first find out any of the two number of greatest common divisor, and then seek this greatest common divisor and the third number of greatest common divisor, in turn, until the last number. The last greatest common divisor is the greatest common divisor of all these numbers.

To find the least common multiple of multiple numbers

A least common multiple of two numbers equals the product of these two numbers divided by the greatest common divisor of these two numbers.

To find the number of least common multiple, it is possible to find the least common multiple of the two numbers, then the least common multiple of the number of the least common multiple and the third number, and so on, until the last number. The last least common multiple is the least common multiple of these few numbers.

Here's the code:

1 functionsmallestcommons (arr) {2arr = Arr.sort (function(A, b) {3     returnA-b;4   });5   varBRR = [];6    for(vari = arr[0]; I <= arr[1]; i++) {7 Brr.push (i);8   }9   varm,n,c,e,f;Ten    One    for(vari = 0; i < brr.length; i++) { A       -      if(Brr[i] <= brr[i+1]) { -m =Brr[i]; then = brr[i+1]; -}Else { -m = brr[i+1]; -n =Brr[i]; +      } -      +      A     //greatest common divisor for two numbers atc = m, E = n, f =C; -       while(c!== 0) { -f = e%C; -E =C; -c =F; -      } in       -      //least common multiple for two numbers to      vard = m * N/e; +      //alert (d); -Brr.splice (0,2, d); the  *  $      if(Brr.length = = 1) {Panax Notoginsengnum =D; -        Break; the      } +i =-1; A   } the  + console.log (num); -   returnnum; $}

The function smallestcommons () receives an array parameter, which consists of two entries, the function of which is to find the least common multiple of successive numbers between the two items, and finally return to the least common multiple.

Like what:

Smallestcommons ([1, 13]); // returns 360360. 

Algorithm--Greatest common divisor

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.