Python math.gcd() メソッド
Python math.gcd()このメソッドは、指定された整数引数の最大公約数を返します。
gcd(0,0) は 0 を返します。
Python バージョン:3.5
バージョン 3.9 で変更: 任意の数の引数のサポートが追加されました。以前のバージョンは2つの引数しかサポートしていませんでした。
構文
math.gcd() メソッドの構文は次のとおりです:
math.gcd(*integers)
引数の説明:
- *integers-- 必須、数値。x が数値でない場合は TypeError を返します。
戻り値
2つ以上の整数の最大公約数 (GCD) を表す整数 int を返します。
実例
次の例では、数値の最大公約数を返します:
実例
# math パッケージをインポート
import math
# 最大公約数を出力
print (math.gcd(3, 6))
print (math.gcd(6, 12))
print (math.gcd(12, 36))
print (math.gcd(-12, -36))
print (math.gcd(5, 12))
print (math.gcd(10, 0))
print (math.gcd(0, 34))
print (math.gcd(0, 0))
import math
# 最大公約数を出力
print (math.gcd(3, 6))
print (math.gcd(6, 12))
print (math.gcd(12, 36))
print (math.gcd(-12, -36))
print (math.gcd(5, 12))
print (math.gcd(10, 0))
print (math.gcd(0, 34))
print (math.gcd(0, 0))
出力結果:
3 6 12 12 1 10 34 0その他の拡張機能
Python math モジュール