Euclidean algorithm for greatest common divisor (GCD)

Source: Internet
Author: User
Tags gcd greatest common divisor

The greatest common divisor algorithm for Euclidean algorithm,

The code is as follows:

int gcd (int a, int b) {if (b = = 0) return A; else gcd (b, a% b  

Prove:

For a, B, there is a = kb + R (A, K, B, r are integers), where r = a mod B.

Make d A and b a convention number, then d|a,d|b (i.e. A, b are evenly divisible by D),

So R =a-kb, both sides divided by D.

R/D = A/D-kb/d = m (M is an integer, because R is also divisible by D)

So I know

The number of conventions and B, a mod b conventions are the same, so they greatest common divisor the same.

That is gcd (A, b) = gcd (b, R)

When r=0, B is obviously (B,R) greatest common divisor, that is, the greatest common divisor of a B,

Euclidean algorithm for greatest common divisor (GCD)

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.