Data structure--seeking greatest common divisor (Euclidean algorithm)

Source: Internet
Author: User
Tags gcd greatest common divisor
Package Com.itany.oulijide;public class test{public        static void Main (string[] args)    {        int result=gcd ( 50,15);        SYSTEM.OUT.PRINTLN (result);    }    Default M>n, if M<n, then the first iteration will exchange the two public    static int gcd (int m,int N)    {while        (n!=0)        {            int rem=m%n;            M=n;            N=rem;        }        return m;    }    }

Run time is logarithmic!


Its calculation principle relies on the following theorem: Theorem: gcd (A, a, b) = gcd (b,a MoD) (A>b and a mod B is not 0) proves that: A can be expressed as A = kb + R, then r = a mod b assumes that D is a number of conventions of a A, then d|a,d|b, and R = A-KB, so d|r D is therefore also (B,a mod b) The number of conventions so (b) and (B,a MoD b) are the same, and their greatest common divisor are necessarily equal,

Data structure--seeking greatest common divisor (Euclidean algorithm)

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.