Greatest common divisor and least common multiple for two numbers

Source: Internet
Author: User
Tags greatest common divisor

Greatest common divisor explanation

Least common multiple explanation

The greatest common divisor and least common multiple of two numbers are calculated, so long as the greatest common divisor can be obtained least common multiple

Two numbers A and B, assuming greatest common divisor is m,a=a1*m,b=b1*m, least common multiple is a1*b1*m= (a*b)/M

Algorithm one poor lifting method

Judging by the order of 1, 2, 3 ..., the largest number that can be evenly divisible by two numbers is greatest common divisor

Improved

Suppose A<b, press A, A-1, a-2 ... Order, the first one to be divisible by two numbers is greatest common divisor

int getgcd (int x, int y) {int i;for (i=x;;; i--) {if (x%i==0&&y%i==0) break;} return i;}

  

Algorithm two-way division method (Euclidean algorithm)

First step: Make R A/b derived remainder (0≤R<B)
If r= 0, the algorithm ends; b is the answer.
Step two: Swap, place A←b,b←r, and return to the first step.

int getgcd (int m,int n) {if (m = = 0| |  n = = 0) return 0; if (M < n) return GETGCD (n, m);    if (m% n = = 0)        return n; else        return GETGCD (n,m% n);}

  

Algorithm Shift Subtraction Loss method

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.

int getgcd (int a,int b) {    while (a!=b)    {        if (a>b)     a-=b;else  b-=a;    }        return A;}

  

The above code just provides ideas that are not validated.

Source: Internal Testing

Greatest common divisor and least common multiple for two numbers

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.