Python | math.gcd() function Last Updated : 20 Feb, 2023 Comments Improve Suggest changes Like Article Like Report In Python, math module contains a number of mathematical operations, which can be performed with ease using the module. math.gcd() function compute the greatest common divisor of 2 numbers mentioned in its arguments. Syntax: math.gcd(x, y) Parameter: x : Non-negative integer whose gcd has to be computed. y : Non-negative integer whose gcd has to be computed. Returns: 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. Time Complexity: O(1) Auxiliary Space: O(1) Code #1: Python3 # Python code to demonstrate the working of gcd() # importing "math" for mathematical operations 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 Code #2: Python3 # Python code to demonstrate the working of gcd() # importing "math" for mathematical operations import math # prints gcd of x, y print ("math.gcd(44, 12) : ", math.gcd(44, 12)) print ("math.gcd(69, 23) : ", math.gcd(65, 45)) Output:math.gcd(44, 12) : 4 math.gcd(69, 23) : 5 Code #3: Explaining Exception. Python3 # 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 ("\nThe 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 : TypeError: 'str' object cannot be interpreted as an integer Comment More infoAdvertise with us Next Article Python | math.gcd() function S Shivam_k Follow Improve Article Tags : Python Python math-library-functions Practice Tags : python Similar Reads Python | sympy.igcd() method With the help of sympy.igcd() method, we can find the greatest common divisor of two nonnegative integer numbers that is passed as a parameter in the sympy.igcd() method. Syntax : sympy.igcd(var1, var2) Return : Return value of greatest common divisor for nonnegative integer. Example #1 : In this ex 1 min read Python | sympy.gcd() method With the help of sympy.gcd() method, we can find the greatest common divisor of two numbers that is passed as a parameter in the sympy.gcd() method. Syntax : sympy.gcd(var1, var2) Return : Return value of greatest common divisor. Example #1 : In this example we can see that by using sympy.gcd() meth 1 min read Python | sympy.gcd() method The function gcd() provides the direct way to compute Greatest Common Divisor for polynomials.That is, for polynomials f and g, it computes GCD. Syntax: sympy.gcd(f, g) Return: GCD of given polynomials Example #1: Python3 1== # import sympy from sympy import * f = (12 * x + 12)*x g = 32 * x**2 # Usi 1 min read numpy.mod() in Python numpy.mod() is another function for doing mathematical operations in numpy.It returns element-wise remainder of division between two array arr1 and arr2 i.e. arr1 % arr2 .It returns 0 when arr2 is 0 and both arr1 and arr2 are (arrays of) integers. Syntax : numpy.mod(arr1, arr2, /, out=None, *, where 2 min read Python | sympy.factorint() method With the help of sympy.factorint() method, we can find the factors and their corresponding multiplicities of a given integer. For input less than 2, factorint() behaves as follows: factorint(1) - returns the empty factorization {}. factorint(0) - returns {0:1}. factorint(-n) - adds -1:1 to the facto 1 min read Like