Skip to content

Commit

Permalink
Create Bézout’s Identity (Extended Euclidean Algorithm)
Browse files Browse the repository at this point in the history
Bézout’s Identity is a fundamental theorem in number theory that states: for any two integers 
𝑎
a and 
𝑏
b with a greatest common divisor 
𝑑
d, there exist two integers 
𝑥
x and 
𝑦
y such that:

𝑎
𝑥
+
𝑏
𝑦
=
𝑑
ax+by=d
The Extended Euclidean Algorithm is an extension of the Euclidean algorithm that not only finds the GCD of two integers 
𝑎
a and 
𝑏
b but also finds the coefficients 
𝑥
x and 
𝑦
y that satisfy Bézout's Identity. This algorithm has practical applications in:

Solving linear Diophantine equations.
Computing modular inverses (important in cryptography).
Finding integer solutions in various number theory problems.
  • Loading branch information
ananyashinde2434 authored Nov 8, 2024
1 parent 386f125 commit 203fc82
Showing 1 changed file with 35 additions and 0 deletions.
35 changes: 35 additions & 0 deletions Bézout’s Identity (Extended Euclidean Algorithm)
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#include <stdio.h>

// Function to implement the Extended Euclidean Algorithm
int extended_euclidean(int a, int b, int *x, int *y) {
// Base case
if (b == 0) {
*x = 1;
*y = 0;
return a;
}

int x1, y1; // Temporarily hold results
int gcd = extended_euclidean(b, a % b, &x1, &y1);

// Update x and y using results of recursive call
*x = y1;
*y = x1 - (a / b) * y1;

return gcd;
}

int main() {
int a, b;
printf("Enter two integers: ");
scanf("%d %d", &a, &b);

int x, y;
int gcd = extended_euclidean(a, b, &x, &y);

printf("GCD of %d and %d is %d\n", a, b, gcd);
printf("Coefficients x and y are: x = %d, y = %d\n", x, y);
printf("Verification: %d * (%d) + %d * (%d) = %d\n", a, x, b, y, gcd);

return 0;
}

0 comments on commit 203fc82

Please sign in to comment.