- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
C Program for Extended Euclidean algorithms?
Here we will see the extended Euclidean algorithm implemented using C. The extended Euclidean algorithm is also used to get the GCD. This finds integer coefficients of x and y like below −
𝑎𝑥+𝑏𝑦 = gcd(𝑎,𝑏)
Here in this algorithm it updates the value of gcd(a, b) using the recursive call like this − gcd(b mod a, a). Let us see the algorithm to get the idea
Algorithm
EuclideanExtended(a, b, x, y)
begin if a is 0, then x := 0 y := 1 return b end if gcd := EuclideanExtended(b mod a, a, x1, y1) x := y1 – (b/a)*x1 y := x1 return gcd end
Example
#include <stdio.h> int EuclideanExtended(int a, int b, int* x, int* y) { if (a == 0) { *x = 0; *y = 1; return b; } int xtemp, ytemp; // To store results of recursive call int res = EuclideanExtended(b % a, a, &xtemp, &ytemp); *x = ytemp - (b / a) * xtemp; *y = xtemp; return res; } int main() { int x, y; int a = 60, b = 25; int res = EuclideanExtended(a, b, &x, &y); printf("gcd(%d, %d) = %d", a, b, res); }
Output
gcd(60, 25) = 5
- Related Articles
- Python Program for Extended Euclidean algorithms
- C Program for Basic Euclidean algorithms?
- Python Program for Basic Euclidean algorithms
- C++ Program to Implement Extended Euclidean Algorithm
- Euclidean Algorithm for calculating GCD in JavaScript
- Introduction to Algorithms for Mathematical Problems
- Array algorithms in C++ STL
- Extended Midy's theorem in C++
- Extended Operators in Relational Algebra in C++
- Calculating Euclidean distance using SciPy
- Rebalancing Algorithms
- Extended Integral Types (Choosing the correct integer size in C/C++)
- Preparing encoding and decoding algorithms for shortening URLs in JavaScript
- C Program for Program for array rotation?
- Find HCF of two numbers without using recursion or Euclidean algorithm in C++

Advertisements