0% found this document useful (0 votes)
30 views2 pages

GCD

This C program calculates the greatest common divisor (GCD) and Bezout coefficients of two integers entered by the user. It uses the Euclidean algorithm to iteratively calculate the GCD. It then uses the results to find the Bezout coefficients, which satisfy the Bezout identity relating the two numbers and their GCD. The program prints out the calculated GCD and Bezout coefficients.

Uploaded by

iman
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
30 views2 pages

GCD

This C program calculates the greatest common divisor (GCD) and Bezout coefficients of two integers entered by the user. It uses the Euclidean algorithm to iteratively calculate the GCD. It then uses the results to find the Bezout coefficients, which satisfy the Bezout identity relating the two numbers and their GCD. The program prints out the calculated GCD and Bezout coefficients.

Uploaded by

iman
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

9/27/22, 11:29 AM www.josephboutros.org/ecen210/classroom/fall2019/gcd.

/***

Last change: 8 June 2019

***

Created: 5 June 2019

***

First C program about GCD and Bezout inequality.

***

***/

#include <stdio.h>

int main(int argc, char** argv)

int m, n, a, b, q, r;

int s, t, new_s, new_t, old_s, old_t, gcd;

printf("Enter your first favorite number m: ");

scanf("%d", &m);

if((m<=0)||(m>=5000000))

fprintf(stderr,"%s: m out of range.\n", argv[0]);

return(-1);

printf("Enter your second favorite number n: ");

scanf("%d", &n);

if((n<=0)||(n>=5000000))

fprintf(stderr,"%s: n out of range.\n", argv[0]);

return(-1);

printf("The gcd of %d and %d is ", m, n);

if(m>=n) { b=m; a=n; } else { b=n; a=m; }

do// Apply Euclidean division. gcd is the last non-zero remainder

r=b % a;

b=a; a=r;

while(r);

printf("%d \n", gcd=b);

// GCD + Bezout

if(m>=n) { b=m; a=n; } else { b=n; a=m; }

s=1; t=0;

old_s=0; old_t=1; //Initialization comes from the particular case above

while(a != 0)

q=b / a; r=b % a;

// because b(i)=b(i-2)-q*b(i-1),

new_s=old_s-q*s; //Coefficient of a in the remainder equation

new_t=old_t-q*t; //Coefficient of b in the remainder equation

old_s=s; old_t=t;

s=new_s; t=new_t;

b=a; a=r;

printf("The gcd between your given numbers is gcd(m,n)=%d \n", gcd);

if(m<=n) printf("The Bezout's pair is (x,y)=(%d, %d) \n", old_s, old_t);

else printf("The Bezout's pair is (x,y)=(%d, %d) \n", old_t, old_s);

www.josephboutros.org/ecen210/classroom/fall2019/gcd.c 1/2
9/27/22, 11:29 AM www.josephboutros.org/ecen210/classroom/fall2019/gcd.c
return(0);

}/* end of main() */

www.josephboutros.org/ecen210/classroom/fall2019/gcd.c 2/2

You might also like