"Algorithm" Euclidean algorithm--seeking greatest common divisor

Source: Internet
Author: User
Tags assert gcd greatest common divisor

Pre-knowledge

Factor (divisor)

If there is an integer n,a,b. A and B are not 0, and there is n = a*b, that is, a (or B, the following ellipsis) is a factor of n, or a can divide n.

In particular: Any non-0 integer is a factor of 0, so generally we do not seek 0 factor.

For example: 3 of the factors are 1,-1, 3,-3. However, we generally only consider the positive factor, because there is no intrinsic difference between negative and positive numbers, only the symbols are different.

Prime Number: prime (also called prime number) is defined as a prime number if the factor of the integer p is only ±1 and ±p. Special: 0 and 1 are neither primes nor composite.

Composite must be decomposed into a number of prime numbers multiplied by the form. such as 3x3x3, 155 = 5x31

Modulo operations

A is a positive integer and can be expressed as: a = Q*m+r (r and Q are integers, and 0<= R < Q), then say: a mod q = r.

Example: 7 mod 4 = 3, because 7= 1*4 +3

We do not give a negative modulo definition because it is not uniformly defined and seldom used. It is important to note, however, that the result of the modulo operation is the same as the symbol for the first operand in C + + and Java. That is, the result of a% B is the same as the sign of a.

Modulo operations are useful, for example, any positive integer mod m will map them to the set {0,1,2,... m-1}.

Euclidean algorithm seeking greatest common divisor

The Convention number operation of A, B is expressed as the return value of the function gcd (A, a, a, a). A, a, is not all 0 non-negative numbers, and a >= B.

Then GCD (A, a, b) = gcd (b, a mod), can be seen as a recursive call, the condition of the end of the call is: The second argument is 0, the result returned is the first argument.

In other words: gcd (m,0) = M.

Code implementation:

intGCD1 (intAintb//use recursion{assert (a>=b); if(b = =0)         returnA; Else         returnGCD1 (b, a%b);}intGCD2 (intAintb//using Loops{assert (a>=b); intR;  while(b! =0) {R=a%b; A=b; b= R; }    returnA;}

"Algorithm" Euclidean algorithm--seeking greatest common divisor

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.