SUMMARY:GCD Greatest common divisor algorithm

Source: Internet
Author: User
Tags gcd greatest common divisor

Euclidean algorithm
Euclidean algorithm, also known as the greatest common divisor method, is used to calculate two integers, a, b, and so on. Its computational principle relies on the following theorem:

Theorem: gcd (b) = gcd (b,a mod b)

Proof: A can be expressed as A = kb + R, then r = a mod b
Assuming D is a number of conventions for a, B, there are
D|a, d|b, and r = a-kb, so d|r
So d is the number of conventions (B,a mod b)

Assuming D is the number of conventions (B,a mod b), then
D | B, D |r, but a = KB +r
So d is also the number of conventions (A, B)

therefore (b) and (b,a mod b) The number of conventions is the same, and their greatest common divisor are necessarily equal, to be certified

Euclid's algorithm is based on this principle, and its algorithm is described in terms of language:

1 int GCD (intint  b)2{3     if(b = = 0)4          return  A; 5     return GCD (b, a% b); 6 }

SUMMARY:GCD Greatest common divisor 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.