Algorithm learning Euclidean algorithm and extended Euclidean algorithm (i.)

Source: Internet
Author: User

This is my algorithm learning tag under the first essay, first I want to declare, after reading a lot of blog, my idea is, try not to blindly copy other people's Things, one reason is that the blind copy may not be able to find the errors in their articles, the second is to deepen their understanding of the level, and the blog is easy to read, Reprint other people's Things must be annotated source. And one of my habits is to write something short, so there's a chance that something that would have been in an article would have been written in several sections.

Euclid algorithm is also called the division of the method, the use is to find two positive integers, a, a greatest common divisor, gcd (A, A, b) represents the greatest common divisor (GCD = greatest common divisor).

Content: GCD (A, B) =gcd (B,A%B);

Condition: a,b>0 (when a%b=0, cannot be used after using the formula)

about Greatest common divisor: the assumption is that for two positive integers , an approximate number must also be a positive integer. The greatest common divisor, 4 8, 4 0 and so on, can be said to be-4 and 8 without greatest common divisor, 4 and 0 without greatest common divisor. Although for the second algorithm to run these two are results,-4 and 4.

Prove:

The expression of a is a=kb+r, then r=a%b, r=a-kb;

Assuming D is a number of conventions for a, B, then a%d = 0, b%d = 0, and r = a-kb, so r% d = 0;

Sort by: b% d = 0, r% d = 0 ((a% b)% d = 0)

That is, D is also the number of conventions for B and a%b, so the greatest common divisor of A and B are also the greatest common divisor of B and a%b.

Recursive form code:

#include <iostream>using namespacestd;intgcdintAintb) {    if(b==0)        returnA; returnGCD (b,a%b);}intMain () {intb; cout<<"Please enter the integers:"<<Endl; CIN>>a>>b; cout<<a<<" and"<<b<<"' s GCD is:"<<Endl; cout&LT;&LT;GCD (b) <<Endl;return 0;}

Non-recursive form:

#include <iostream>using namespacestd;intgcdintAintb) {    inttemp;  while(b!=0) {Temp=b; b=a%b; A=temp; }    returnA;}intMain () {intb; cout<<"Please enter the integers:"<<Endl; CIN>>a>>b; cout<<a<<" and"<<b<<"' s GCD is:"<<Endl; cout&LT;&LT;GCD (b) <<Endl;return 0;}

Run:

There are two points to note:

1. The implication of similar gcd (4,0) does not directly represent gcd (4,0) = 4, since the number of Conventions 4 and 0 does not exist. But by the front, got gcd (4,0), a number mod 4 = 0, that is, the number is 4 whole except, so greatest common divisor is 4.

2. There are a lot of code on the net first to determine the size of a and B, a<b time, to exchange A and B values. In fact, when A<b, temp=b; B=a%b;  A=temp; is a swap process, so swapping positions is not necessary. Ask GCD (4,8) =gcd (8,4) = ....

Algorithm learning Euclidean algorithm and extended Euclidean algorithm (i.)

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.