Python math.factorial() メソッド
Python math.factorial(x)このメソッドは x の階乗を返します。
引数には正の整数のみ指定できます。
ある数の階乗は、その数以下のすべての正の整数の積です。例えば、6 の階乗は次のとおりです。 6 x 5 x 4 x 3 x 2 x 1 = 720。
構文
math.factorial() メソッドの構文は次のとおりです。
math.factorial(x)
パラメータの説明:
- x-- 必須、正の整数。数値が負の数または整数でない場合は ValueError を返します。値が数値でない場合は TypeError を返します。
戻り値
正の整数の階乗を表す正の整数を返します。
例
次の例では、正の整数の階乗を返します。
例
# math パッケージをインポート
import math
# 正の整数の階乗を出力
print(math.factorial(9))
print(math.factorial(6))
print(math.factorial(12))
import math
# 正の整数の階乗を出力
print(math.factorial(9))
print(math.factorial(6))
print(math.factorial(12))
出力結果:
362880 720 479001600その他の拡張
Python math モジュール