gcd() in Python
The Highest Common Factor (HCF) , also called gcd, can be computed in python using a single function offered by math module and hence can make tasks easier in many situations.
- Using Recursion:
- Using Loops
# Python code to demonstrate naive# method to compute gcd ( Loops )defcomputeGCD(x, y):ifx > y:small=yelse:small=xforiinrange(1, small+1):if((x%i==0)and(y%i==0)):gcd=ireturngcda=60b=48# prints 12print("The gcd of 60 and 48 is : ",end="")print(computeGCD(60,48))chevron_rightfilter_noneOutput:
The gcd of 60 and 48 is : 12
- Using Euclidean Algorithm
# Python code to demonstrate naive# method to compute gcd ( Euclidean algo )defcomputeGCD(x, y):while(y):x, y=y, x%yreturnxa=60b=48# prints 12print("The gcd of 60 and 48 is : ",end="")print(computeGCD(60,48))chevron_rightfilter_noneOutput:
The gcd of 60 and 48 is : 12
- Both numbers are 0, gcd is 0
- If only either number is Not a number, Type Error is raised.
- Python | Merge Python key values to list
- Python | Index of Non-Zero elements in Python list
- Important differences between Python 2.x and Python 3.x with examples
- Reading Python File-Like Objects from C | Python
- Python | Convert list to Python array
- Python | Add Logging to Python Libraries
- Python | Sort Python Dictionaries by Key or Value
- Python | Set 4 (Dictionary, Keywords in Python)
- Python | Add Logging to a Python Script
- Python | Visualizing O(n) using Python
- JavaScript vs Python : Can Python Overtop JavaScript by 2020?
- bin() in Python
- Any & All in Python
- max() and min() in Python
- Python vs PHP
# Python code to demonstrate naive # method to compute gcd ( recursion ) def hcfnaive(a,b): if(b==0): return a else: return hcfnaive(b,a%b) a = 60b= 48 # prints 12 print ("The gcd of 60 and 48 is : ",end="") print (hcfnaive(60,48)) |
Output:
The gcd of 60 and 48 is : 12
Using gcd() can compute the same gcd with just one line.
math.gcd( x, y ) Parameters : x : Non-negative integer whose gcd has to be computed. y : Non-negative integer whose gcd has to be computed. Return Value : This method will return an absolute/positive integer value after calculating the GCD of given parameters x and y. Exceptions : When Both x and y are 0, function returns 0, If any number is a character , Type error is raised.
# Python code to demonstrate gcd() # method to compute gcd import math # prints 12 print ("The gcd of 60 and 48 is : ",end="") print (math.gcd(60,48)) |
Output:
The gcd of 60 and 48 is : 12
Some common Exceptions in this function are :
# Python code to demonstrate gcd() # method exceptions import math # prints 0 print ("The gcd of 0 and 0 is : ",end="") print (math.gcd(0,0)) # Produces error print ("The gcd of a and 13 is : ",end="") print (math.gcd('a',13)) |
Output:
The gcd of 0 and 0 is : 0 The gcd of a and 13 is :
Runtime Error :
Traceback (most recent call last):
File "/home/94493cdfb3c8509146254862d12bcc97.py", line 12, in
print (math.gcd('a',13))
TypeError: 'str' object cannot be interpreted as an integer
This article is contributed by Manjeet Singh. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.




