Movatterモバイル変換


[0]ホーム

URL:


Open In App

Given two numbers a and b, the task is to find their Extended GCD, i.e., the greatest common divisor g, and integers x and y such that: ax+by = g. This is known as Bézout’s identity, and it’s useful for solving linear Diophantine equations and finding modular inverses.

Let’s explore different ways to find the Extended GCD in Python.

Using Iterative Extended Euclidean Algorithm

This approach uses a loop to calculate both gcd and coefficients x, y such that ax + by = gcd(a, b). It avoids recursion stack usage and is memory-efficient.

Python
a,b=35,15x0,x1,y0,y1=1,0,0,1whileb:q=a//ba,b=b,a%bx0,x1=x1,x0-q*x1y0,y1=y1,y0-q*y1print("GCD is",a)print("x =",x0,", y =",y0)

Output
GCD is 5x = 1 , y = -2

Explanation:

  • x0, x1 and y0, y1 track coefficients for the equation.
  • q stores the quotient of division (a // b).
  • At each step, values are updated based on Euclid’s algorithm logic.
  • Once b becomes 0, a is the GCD, and x0, y0 are the required coefficients.

Using Recursive Extended Euclidean Algorithm

This is the traditional recursive approach that returns gcd, x, and y. It works by breaking down the problem recursively until the base case is reached.

Python
defgcdExtended(a,b):ifa==0:returnb,0,1gcd,x1,y1=gcdExtended(b%a,a)x=y1-(b//a)*x1y=x1returngcd,x,ya,b=35,15g,x,y=gcdExtended(a,b)print("GCD is",g)print("x =",x,", y =",y)

Output
GCD is 5x = 1 , y = -2

Explanation:

  • The function calls itself recursively with the remainder until a becomes 0.
  • Backtracking computes the coefficients x and y satisfying the equation.
  • Final values of x, y form the Bézout coefficients.

Using Matrix Representation

This approach represents the coefficient updates as matrix multiplications, which can be useful for understanding and debugging iterative updates.

Python
a,b=35,15x,y,u,v=0,1,1,0whilea!=0:q,r=divmod(b,a)b,a=a,rx,u=u-q*x,xy,v=v-q*y,yprint("GCD is",b)print("x =",u,", y =",v)

Output
GCD is 5x = -2 , y = 1

Explanation:

  • divmod(b, a) gives quotient and remainder simultaneously.
  • Coefficients x, y, u, v track changes similar to matrix operations.
  • Once a becomes zero, b holds the GCD, and u, v are the required coefficients.

Using Built-in math.gcd()

Although this function doesn’t return the coefficients x and y, it’s the fastest way to get the GCD itself. It can serve as a quick validation check.

Python
importmatha,b=35,15print("GCD is",math.gcd(a,b))

Output
GCD is 5

Explanation:math.gcd(a, b) efficiently computes the greatest common divisor using the Euclidean algorithm internally.

Please refer complete article onBasic and Extended Euclidean algorithms for more details!


Improve
Improve
Article Tags :

Explore

Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences

[8]ページ先頭

©2009-2025 Movatter.jp