The oldest algorithm: Euclidean method (two natural number GCD)

Source: Internet
Author: User
Tags gcd

In mathematics, the Euclidean algorithm, also known as Euclid, is considered to be the world's oldest algorithm (300 BC), which is used to find two GCD algorithms. The Euclidean method first appeared in Euclid's "Geometrical Original" (Volume VII, Proposition Yⅰ and Ⅱ), while in China, it can be traced back to the nine chapters of arithmetic which appeared in the Eastern Han Dynasty.

The gcd of two natural numbers is the largest positive integer that can divide them at the same time. The Euclidean method is based on the principle that the gcd of two integers equals the gcd of the smaller number and the dividing remainder of the two number. For example, the GCD of 1254 and 390 is 6 (1254 = 6x209;390 = 6x65); The process of deriving gcd with these two numbers is as follows:

1254% 390 = 84

390% 84 = 54

84% 54 = 30

54% 30 = 24

30% 24 = 6

So the gcd of these two numbers is 6, which is obviously a recursive algorithm.

The proof of this algorithm is as follows:

Set two numbers a, B (B<a), gcd (a,b) for a,b gcd, r=a MoD b for a divided by B after the remainder, K is a divided by B of the quotient. The Euclidean method is to prove that gcd (a,b) =GCD (b,r).

The first step: Make C=GCD (a,b), then set A=MC,B=NC

Step two: According to the premise that R =a-kb=mc-knc= (M-KN) c

Step three: According to the results of the second step, C is also the factor of R

The fourth step: can be concluded that M-KN and N-reciprocity "otherwise, can be set M-kn=xd,n=yd, (d>1), then m=kn+xd=kyd+xd= (ky+x) d, then a=mc= (ky+x) DC,B=NC=YCD, so A and B gcd become CDs, not C, Contradiction with the preceding conclusion "

Thus we can know GCD (b,r) =c, then gcd (a,b) =GCD (b,r).

PS: This conclusion is based on the second step R = (M-KN) c, the first step B =nc takes R into GCD (B,R), gets the GCD (NC, (M-KN) c), so only N and m-kn are prime, and the GCD of B and R is C.

The Java implementation is given below

public class GCD
{public
    static int getgcd (int a, int b)
    {
        if (a < 0 | | b < 0)
            return-1;
        if (a < b)
        {
            int c = b;
            b = A;
            A = C;
        }
        int C = a% B;
        if (c = = 0) return
            b;
        else return
            GETGCD (b, c);
    }
          
    public static void Main (string[] args)
    {
       System.out.println (GETGCD (1254, 390));
    }

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.