math.integer --- integer-specific mathematics functions

在 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)!),否則其值為 0。

因為此值等同於 (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)!,否則其值為 0。

若未給定 k 或其值為 None,則 k 值預設為 n,且函式回傳 n!。

Raises ValueError if either of the arguments are negative.