Greatest common divisor, least common multiple "number theory"

Source: Internet
Author: User
Tags greatest common divisor

Function: Greatest common divisor of A and B

Incoming parameters: integer A, integer b

Outgoing parameters: Greatest common divisor of A and B


Algorithm 1: Euclidean algorithm

Time complexity: O (N)

Implementation principle:

For A, B (a>b) of two, the steps for A and B greatest common divisor (b) are as follows:

B In addition to a, get A÷b=q......r1 (0≤R1). If r1=0, then (A, b) =b;

If r1≠0, then use R1 except B, get B÷r1=q......r2 (0≤R2).

If r2=0, then (A, B) =r1, if r2≠0, then continue to use R2 in addition to R1, ...

So go on until you can divide it evenly. The last non-0 divisor is (A, b).

Implementation code:

#include <iostream> #include <algorithm> #include <cstdio> #include <cstring> using namespace Std;template <typename t>t GCD (t a,t b) {    if (a < b)    {        T temp = A;        A = b;        b = temp;    }    if (b = = 0)        return A;    Return GCD (b,a%b);} int main () {    int a = 5,b = 6;    printf ("%d\n", GCD (A, b));    return 0;}

Algorithm 2:stein algorithm

Time complexity: O (log (max (b)))

Implementation principle:

GCD (A,a) =a, that is, a number and its own number of conventions is still its own.
GCD (KA,KB) =k gcd (A, b), that is, the greatest common divisor operation and the multiply multiplication operation can be exchanged.

In particular, when k=2, two even-numbered greatest common divisor must be divisible by 2.
When K and B are mutually prime, gcd (ka,b) =GCD (A, b), that is, about two digits, only one of them

A contained factor does not affect the greatest common divisor. Specifically, when k=2, the description calculates a

When even and an odd greatest common divisor, you can divide the even number by 2.

Algorithm process:

1, if an=bn, then an (or Bn) *CN is greatest common divisor, the algorithm ends
2. If an=0,bn is greatest common divisor, the algorithm ends
3. If Bn=0,an is greatest common divisor, the algorithm ends
4. Set A1=a, B1=b and C1=1
5, if an and Bn are even, then an+1=an/2,bn+1=bn/2,cn+1=cn*2 (note, multiply 2 to move the integer to the left one can, except 2 to move the integer right one can)
6, if an is even, Bn is not even, then AN+1=AN/2,BN+1=BN,CN+1=CN (obviously, 2 is not an odd number of approximate)
7, if the Bn is even, an is not even, then BN+1=BN/2,AN+1=AN,CN+1=CN (obviously, 2 is not an odd number of approximate)
8, if an and bn are not even, then an+1=| An-bn|/2,bn+1=min (an,bn), CN+1=CN
9, N plus 1, ext. 1

Implementation code:

#include <iostream> #include <algorithm> #include <cstdio> #include <cstring>using namespace Std;template <typename t>t GCD (T a,t b) {    if (a = = 0) return b;    if (b = = 0) return A;    if (~a & 1)    {        if (b & 1)            return GCD (a >> 1, b);        else            return GCD (a >> 1, b >> 1) << 1;    }    if (~b & 1)        return GCD (A, B >> 1);    if (a > B)        return GCD ((a) >> 1, b);    

Max Common multiple:

Implementation code:

Template <typename t>t LCM (T a,t b) {return a/gcd (a b) *b;}


Greatest common divisor, least common multiple "number theory"

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.