Open In App

Python Program for Extended Euclidean algorithms

Last Updated : 21 Jun, 2022
Comments
Improve
Suggest changes
Like Article
Like
Report
Python3
# Python program to demonstrate working of extended 
# Euclidean Algorithm 
    
# function for extended Euclidean Algorithm 
def gcdExtended(a, b): 
    # Base Case 
    if a == 0 : 
        return b,0,1
            
    gcd,x1,y1 = gcdExtended(b%a, a) 
    
    # Update x and y using results of recursive 
    # call 
    x = y1 - (b//a) * x1 
    y = x1 
    
    return gcd,x,y 
    

# Driver code 
a, b = 35,15
g, x, y = gcdExtended(a, b) 
print("gcd(", a , "," , b, ") = ", g) 

Output:

gcd(35, 15) = 5

Time Complexity: O(log(max(A, B)))

Auxiliary Space: O(log(max(A, B))), keeping recursion stack in mind.

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


Article Tags :
Practice Tags :

Similar Reads

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy