math.integer — integer-specific mathematics functions¶
Added in version 3.15.
This module provides access to the mathematical functions defined for integer arguments.
These functions accept integers and objects that implement the
__index__() method which is used to convert the object to an integer
number.
The following functions are provided by this module. Unless stated otherwise below, all return values are computed exactly and are integers.
- math.integer.comb(n, k, /)¶
Return the number of ways to choose k items from n items without repetition and without order.
Evaluates to
n! / (k! * (n - k)!)whenk <= nand evaluates to zero whenk > n.Also called the binomial coefficient because it is equivalent to the coefficient of k-th term in polynomial expansion of
(1 + x)ⁿ.Raises
ValueErrorif either of the arguments are negative.
- math.integer.factorial(n, /)¶
Return factorial of the nonnegative integer n.
- math.integer.gcd(*integers)¶
Return the greatest common divisor of the specified integer arguments. If any of the arguments is nonzero, then the returned value is the largest positive integer that is a divisor of all arguments. If all arguments are zero, then the returned value is
0.gcd()without arguments returns0.
- math.integer.isprime(n, /)¶
Return
Trueif n is a prime number,Falseotherwise.A prime number is a natural number greater than 1 that is not a product of two smaller natural numbers. Negative numbers,
0and1are not prime.The argument must be less than 264; raises
OverflowErrorotherwise.Added in version 3.16.0a0 (unreleased).
- math.integer.isqrt(n, /)¶
Return the integer square root of the nonnegative integer n. This is the floor of the exact square root of n, or equivalently the greatest integer a such that a² ≤ n.
For some applications, it may be more convenient to have the least integer a such that n ≤ a², or in other words the ceiling of the exact square root of n. For positive n, this can be computed using
a = 1 + isqrt(n - 1).
- math.integer.lcm(*integers)¶
Return the least common multiple of the specified integer arguments. If all arguments are nonzero, then the returned value is the smallest positive integer that is a multiple of all arguments. If any of the arguments is zero, then the returned value is
0.lcm()without arguments returns1.
- math.integer.perm(n, k=None, /)¶
Return the number of ways to choose k items from n items without repetition and with order.
Evaluates to
n! / (n - k)!whenk <= nand evaluates to zero whenk > n.If k is not specified or is
None, then k defaults to n and the function returnsn!.Raises
ValueErrorif either of the arguments are negative.
- math.integer.primes(start=2, stop=None)¶
Return an iterator of the prime numbers p with
start <= p < stop, in increasing order. If stop isNone(the default), the iteration does not stop.Roughly equivalent to
(p for p in itertools.count(start) if isprime(p))for the unbounded form.The bounds must be less than 264; raises
OverflowErrorotherwise. Unbounded iteration raisesOverflowErrorif the candidates reach that limit.Added in version 3.16.0a0 (unreleased).