Python Program for Basic Euclidean algorithms

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python program to demonstrate Basic Euclidean Algorithm
  
  
# Function to return gcd of a and b
def gcd(a, b): 
    if a == 0 :
        return
      
    return gcd(b%a, a)
  
a = 10
b = 15
print("gcd(", a , "," , b, ") = ", gcd(a, b))
  
a = 35
b = 10
print("gcd(", a , "," , b, ") = ", gcd(a, b))
  
a = 31
b = 2
print("gcd(", a , "," , b, ") = ", gcd(a, b))
  
# Code Contributed By Mohit Gupta_OMG <(0_o)>

chevron_right


Output:

GCD(10, 15) = 5
GCD(35, 10) = 5
GCD(31, 2) = 1

Time Complexity: O(Log min(a, b))

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



My Personal Notes arrow_drop_up


Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.