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. All return values are computed exactly and are integers.

math.integer.comb(n, k, /)

返回不重复且无顺序地从 n 项中选择 k 项的方式总数。

k <= n 时取值为 n! / (k! * (n - k)!);当 k > n 时取值为零。

也称为二项式系数,因为它等价于 (1 + x)ⁿ 的多项式展开中第 k 项的系数。

Raises ValueError if either of the arguments are negative.

math.integer.factorial(n, /)

返回非负整数 n 的阶乘。

math.integer.gcd(*integers)

返回给定的整数参数的最大公约数。 如果有一个参数非零,则返回值将是能同时整除所有参数的最大正整数。 如果所有参数为零,则返回值为 0。 不带参数的 gcd() 返回 0

math.integer.isqrt(n, /)

返回非负整数 n 的整数平方根。 这就是对 n 的实际平方根向下取整,或者相当于使得 a² ≤ n 的最大整数 a

对于某些应用来说,可以更适合取值为使得 n ≤ a² 的最小整数 a ,或者换句话说就是 n 的实际平方根向上取整。 对于正数 n,这可以使用 a = 1 + isqrt(n - 1) 来计算。

math.integer.lcm(*integers)

返回给定的整数参数的最小公倍数。 如果所有参数均非零,则返回值将是为所有参数的整数倍的最小正整数。 如果参数之一为零,则返回值为 0。 不带参数的 lcm() 返回 1

math.integer.perm(n, k=None, /)

返回不重复且有顺序地从 n 项中选择 k 项的方式总数。

k <= n 时取值为 n! / (n - k)!;当 k > n 时取值为零。

如果 k 未指定或为 None,则 k 默认值为 n 并且函数将返回 n!

Raises ValueError if either of the arguments are negative.