Euclidean algorithm--the method of dividing

Source: Internet
Author: User
Tags greatest common divisor

Today is doing a very simple algorithm topic, "Beg greatest common divisor and least common multiple". A look, too TM easy.

The thinking process is this:

1. Greatest common divisor, there are two extremes, one is greatest common divisor is 1, and one greatest common divisor is the smaller number between two numbers.

2. I take it for granted that so easy. Isn't it just a for-loop? From the smaller to the 1 of the range, if one of the number can be divided by a larger number of OK ROM, and then return to that number, do not get greatest common divisor.

3. Then dash to write down the following code

 public  static  void  commondivisor (int  Span style= "color: #000000;" > m) { //  where N,m is the number to be asked, and when n%i modulo equals 0, the When the m%i equals 0, I is m and N greatest common divisor  for  (int  i=n;i>0;i-- if  (n %i==0&&m%i==0 + "and" +m+ "greatest common divisor                Yes: "+i);             break  ; }                    }    }

--------------------------------------------------the shame of the dividing line--------------------------------------------------------------

I see the answer on the Internet the whole person is drunk, what does this TM mean?

 Public Static int gcd (int m,int  N)    {        while(true)        {             if ((m=m%n) ==0)                 return N;             if ((n=n%m) ==0)                 return m;        }    }

See this code, suddenly found this is I have learned a mathematical theorem---Euclidean algorithm (greatest common divisor), you can get the fastest, the specific derivation process will not say. Http://baike.baidu.com/view/1241014.htm?fr=aladdin

I used to think that math is not very useful in programming (forgive me for being a rookie anyway). Now there is a big difference in the ideas contained in the two-paragraph simple code. I am writing from the time complexity is O (n), on the net is 5log (n).

One with brute force, a skillful exertion.

A simple brute, one standing on the shoulders of giants.

A time of great complexity, one so happy single.

Euclidean algorithm--the method of dividing

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.