Python math.gcd()メソッド
例
2つの整数の最大公約数を見つけます。
#Import math Library
import math
#find the the greatest
common divisor of the two integers
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))
定義と使用法
このメソッドは、2つの整数int1とint2math.gcd()
の最大公約数を返します。
GCDは、余りなしで数値を除算する最大公約数です。
GCDは、最大公約数(HCF)としても知られています。
ヒント: gcd(0,0)は0を返します。
構文
math.gcd(int1, int2)
パラメータ値
Parameter | Description |
---|---|
int1 | Required. The first integer to find the GCD for |
int2 | Required. The second integer to find the GCD for |
技術的な詳細
戻り値: | 2つのint 整数の最大公約数(GCD)を表す値 |
---|---|
Pythonバージョン: | 3.5 |