Euclidean algorithm, greatest common divisor

Source: Internet
Author: User
Tags gcd greatest common divisor

Its calculation principle relies on the following theorem: Theorem: gcd (A, a, b) = gcd (b,a MoD) (A>b and a mod B is not 0) proves that: A can be expressed as A = kb + R, then r = a mod b assumes that D is a number of conventions of a A, then d|a,d|b, and R = A-KB, so d|r D is therefore also (B,a mod b) The number of conventions so (b) and (b,a mod b) The number of conventions is the same, and its greatest common divisor is necessarily equal, provable or: proof: First step: Make C=GCD (A, b), then set a=mc,b= NC Second step: According to the premise of the R =a-kb=mc-knc= (M-KN) C third step: According to the second step results that C is the factor of R Fourth step: can determine m-kn and n reciprocity "otherwise, can be set M-kn=xd,n=yd, (d>1), then m=kn+xd=kyd+ Xd= (ky+x) d, then a=mc= (ky+x) DC,B=NC=YCD, so A and B greatest common divisor ≥cd, rather than C, with the previous conclusion contradiction "so that gcd (B,r) =c, and then gcd (A, A, A, b) =gcd (B,R), the two methods are the same in essence.

#include<stdio.h>

int GCD (int M,int N)

{

int temp;

while (N > 0) {

temp = M% N;

M = N;

N = temp;

}

return M

}


int Main ()

{

int A, b;

scanf ("%d%d", &a,&b);

printf ("%d",Gcd(A, b));

return 0;

}

Euclidean algorithm, 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.