Python math.gcd() Method
Find the greatest common divisor of the two integers:
#Import math Library
import math
#find the the greatest common divisor of the two integers
print (math.gcd(3, 6))
print (math.gcd(6, 12))
print (math.gcd(12, 36))
print (math.gcd(-12, -36))
print (math.gcd(5, 12))
print (math.gcd(10, 0))
print (math.gcd(0, 34))
print (math.gcd(0, 0))
Definition and Usage
The math.gcd() method returns the greatest common divisor of the two integers int1 and int2.
GCD is the largest common divisor that divides the numbers without a remainder.
Math gcd python что это
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)
Top 10 math functions in python
![]()
The math module provides us various functions. Some of them are discussed in this article. Import the math function to begin.
The ten methods in this article are
- ceil and floor
- factorial
- gcd
- sqrt
- pow
- sin, cos, tan
- exp
- fabs
- isclose
- constants
Ceil and floor functions
The ceil() method returns an integer that is greater than or equal to the given float value. Similarly, the floor() method returns an integer that is lesser or equal to the given float value. If an integer value is given the same value is returned.
factorial
This method returns the factorial value of a number. In mathematics, the factorial of a positive integer n, denoted by n! is the product of all positive integers less than or equal to n.
Example 4! is 4*3*2*1 and 5! is 5*4*3*2*1
The gcd() method returns the gcd of the two numbers passed to the function. In mathematics, the greatest common divisor (gcd) of two or more integers, which are not all zero, is the largest positive integer that divides each of the integers. For example, the gcd of 8 and 12 is 4.
The sqrt() method returns the square root of a number. In mathematics, a square root of a number x is a number y such that y² = x.
The pow(x, y) method returns the value of x raised to the power of y. The return type is always float and hence takes both integer or float as arguments.
sin, cos, and tan
The sin, cos, and tan methods take a degree and return their corresponding value.
Return e raised to the power x, where e = 2.718281… is the base of natural logarithms.
The fabs() method returns the absolute value of the number as a floating-point value. This is similar to the abs() method except for the result here is in float.
isclose
The isclose() method takes two values. We can specify the value of the difference. A difference value greater than this returns False and a difference value less than this returns True. By default, the difference value is 0.
The difference value can be specified as a relative or absolute difference.
The first command returns false as the difference between 2 and 5 is 3, but the difference that we have specified is 1.
Whereas the second command returns true as the difference between 2 and 3 is 1, which matches our difference.
Constants
The values of mathematical constants such as π, Τ, and e are also available in the math module.
How to use the math.gcd() function in Python
Many candidates are rejected or down-leveled in technical interviews due to poor performance in behavioral or cultural fit interviews. Ace your interviews with this free course, where you will practice confidently tackling behavioral interview questions.
The Math Module has many functions in Python for mathematical operations. math.gcd() is a function that gives us the greatest common divisor of two numbers when given.
Syntax
Parameter
This function accepts two parameters, x and y , to calculate the greatest common divisor of two parameters.
Output
This function outputs the greatest common divisor of two numbers that are passed as parameters to the function.