Euclidean algorithm: From the proof equation gcd (m, n) = gcd (n, M mod n) for each pair of positive integers m, n is set to go

Source: Internet
Author: User
Tags gcd greatest common divisor

When writing poems or programs, we often have to deal with Euclid's algorithms. However, there is no reason why Euclid's algorithms are effective and efficient, and some extreme (well, allow me to use this strong personal emotional word) computer scientists believe that unless the correctness of the program is mathematically completely strictly confirmed, otherwise we can not think that the program is correct. Since the existence is reasonable, so I will explain in detail the Euclidean algorithm, why it is the correct algorithm (algorithm process is not given, with the idea, whether it is iterative or circular implementation should not be problematic), why there is so good time complexity.

The first is to prove the proposition: note that the proposition proves the correctness of Euclid's algorithm.

Proof: Make d = gcd (M, N), then d|m and D|n. Set m = kn + R (0≤r < n), then d| (Kn+r). There are also d|n, so d|kn, so there are d|r. That is, we from D|m and d|n This premise can be derived d|r. In other words, we can also say that by D|m and d|n this premise introduced D|n and D|r. Using a similar reasoning process can also be obtained: by D|n and D|r can be launched D|m and D|n. Notice here that R is the M mod N. So we can say that gcd (m, n) = gcd (n, M mod n) is two-way established, the proposition is proved.

About Euclid theorem I still want to say a little more, that is Euclid himself to give the process of proof, his thinking clear, logical, although the use of geometric proof method, but not yet strict and beautiful. I would like to briefly repeat our beloved Euclid's proof that the original proves to be seen in Euclid's famous work "geometry originally": If M and n are greatest common divisor, then they are obviously 1. If the two are not the same: first, imagine two segments, their lengths are M and N respectively. Let's call them line AB and CD. We take one of the shorter part of a paragraph, let's think that CD is one of the shorter segment, if the CD volume of AB, then it is also the amount of its own, then CD is CD, AB, a public degree, obviously CD is also the largest number of public degrees, because there is no larger than the CD of energy to do CD. However, if the CD volume is not an AB, then the smaller is subtracted from the larger of the AB,CD, so that there will be a remainder of the energy to do the previous one. We say this number is the largest male of M and N, why? assuming that the remainder is not the maximum male , then there must be a number greater than that number of energy to do AB and CD, and finally we can roll out this number of energy greater than the remainder to do this remainder, noting that this is not possible, because the larger number of the smaller number is not possible. Through this counter-proof process, we know that there is no more than the remainder of the number of energy to do AB,CD. So this number is the largest male of AB and CD, which is M and N.

Well, to prove that we're done here, let's examine the efficiency of Euclid's algorithm.

Make r0 = m mod n, r1 = n mod r0. So there are gcd (M, n) = GCD (n, r0) = gcd (R0, R1). Just note this fact: R0 < M/2 or R1 < N/2. It is possible to know that the size of the algorithm is halved in two steps, that is to say, its time complexity is 2logn = O (Logn). This is the upper bound of its time complexity. If you write an algorithm casually, its time complexity is O (logn), then the world is how beautiful ah.

Euclidean algorithm: From the proof equation gcd (m, n) = gcd (n, M mod n) for each pair of positive integers m, n is set to go

Related Article

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.