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 pow() Function - Python pow() function in Python is a built-in tool that calculates one number raised to the power of another. It also has an optional third part that gives the remainder when dividing the result. Example:Pythonprint(pow(3,2))Output9 Explanation: pow(3, 2) calculates 32 = 9, where the base is positive and t 2 min read 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 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. Naive Methods to compute gcdWay 1: Using Recursion Python3 # Python code to demonstrate naive # method to compute gcd ( recur 2 min read divmod() in Python and its application In Python, divmod() method takes two numbers and returns a pair of numbers consisting of their quotient and remainder. In this article, we will see about divmod() function in Python and its application. Python divmod() Function Syntaxdivmod(x, y)x and y : x is numerator and y is denominatorx and y m 4 min read Modulo operator (%) in Python Modulo operator (%) in Python gives the remainder when one number is divided by another. Python allows both integers and floats as operands, unlike some other languages. It follows the Euclidean division rule, meaning the remainder always has the same sign as the divisor. It is used in finding even/ 4 min read numpy.gcd() in Python numpy.gcd() function computes the greatest common divisor (GCD) of two integers element-wise. The GCD of two numbers is the largest positive integer that divides both numbers without leaving a remainder.Pythonimport numpy as np res = np.gcd(36, 60) print(res)Output12 The GCD of 36 and 60 is 12, whic 2 min read Like