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
ValueErrorif 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
ValueErrorif either of the arguments are negative.