Explanation of the greatest common divisor GCD algorithm (Euclidean proof)

Greatest Common Divisor(GCD)

Euclid's algorithm is said to be the earliest algorithm used to calculate the greatest common divisor and one of the basic algorithms of number theory.

1. The idea of ​​Euclidean algorithm:

The idea of ​​Euclid's algorithm is based on the principle of tossing and dividing, which is the core idea of ​​Euclid's algorithm. To put it bluntly, Euclid's algorithm is actually the realization of the computer algorithm of tossing and dividing. Let's talk about the tossing and turning division method first: If gcd(a,b) is used to represent the greatest common divisor of a and b , then according to the principle of the tossing and turning division method, there is gcd(a,b)=gcd (b,a mod (b)) , where mod() represents modulo operation, and may wish to let a>b, which is convenient for modulo arithmetic.


 

2. Proof of the correctness of the tossing and turning division method gcd(a,b)=gcd(b,a mod (b)) :

Step 1: Let c be the greatest common divisor of a and b , expressed in mathematical notation as c=gcd(a,b). Because the greatest common divisor c of any two real numbers must exist, that is to say, there must be two Number k1, k2 such that a=k1.c, b=k2.c

Step 2: a mod (b) is equivalent to the existence of integers r, k3 such that the remainder r=a – k3.b.

             Immediately r = a – k3.b

            = k1.c – k3.k2.c

            = (k1 – k3.k2).c

        Obviously, the remainder r of a and b is a multiple of the greatest common factor c .

3. Advantages of Euclidean algorithm:

Through the relationship that the remainder of the modulo operation is an integer multiple of the greatest common divisor, the dimensionality reduction of relatively large numbers is convenient for hand calculation; at the same time, the judgment test of the global greatest common divisor is avoided in the feasible interval. , the greatest common divisor can be directly obtained by only selecting the remaining numbers and performing corresponding calculations, which greatly improves the operation efficiency.


Here are the recursive and non-recursive procedures for finding the greatest common divisor using the Euclidean algorithm , and the procedure for finding the greatest common divisor by the exhaustive method .

In terms of computing time, the recursive method is the fastest.

Conditional compilation statements are included in the program for statistical analysis of computational complexity.

/* 
* Three algorithms for calculating the greatest common divisor of two numbers 
*/

#include <stdio.h>

//#define DEBUG
#ifdef DEBUG
int c1=0, c2=0, c3=0;
#endif

int gcd1(int, int);
int gcd2(int, int);
int gcd3(int, int);

int main(void)
{
    int m=42, n=140;

    printf("gcd1: %d %d result=%d\n", m, n, gcd1(m, n));
    printf("gcd2: %d %d result=%d\n", m, n, gcd2(m, n));
    printf("gcd3: %d %d result=%d\n", m, n, gcd3(m, n));
#ifdef DEBUG
    printf("c1=%d  c2=%d  c3=%d\n", c1, c2, c3);
#endif

    return 0;
}

/* 递归法:欧几里得算法,计算最大公约数 */
int gcd1(int m, int n)
{
#ifdef DEBUG
    c1++;
#endif
    return (m==0)?n:gcd1(n%m, m);
}

/* 迭代法(递推法):欧几里得算法,计算最大公约数 */
int gcd2(int m, int n)
{
    while(m>0)
    {
#ifdef DEBUG
    c2++;
#endif
        int c = n % m;
        n = m;
        m = c;
    }
    return n;
}

/* 连续整数试探算法,计算最大公约数 */
int gcd3(int m, int n)
{
    if(m>n) {
        int temp = m;
        m = n;
        n = temp;
    }
    int t = m;
    while(m%t || n%t)
    {
#ifdef DEBUG
    c3++;
#endif
        t--;
    }
    return t;
}


转载:https://blog.csdn.net/tigerisland45/article/details/51151529

推荐:https://www.cnblogs.com/frog112111/archive/2012/08/19/2646012.html

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325685606&siteId=291194637