Python 最大公約数アルゴリズム

Document 对象参考手册Python3 実例

以下のコードは最大公約数アルゴリズムを実装するためのものです:

実例(Python 3.0+)

# Filename : test.py # author by : www.example.com # 関数を定義する def hcf(x, y): """この関数は2つの数の最大公約数を返します""" # 最小値を取得 if x > y: smaller = y else: smaller = x for i in range(1,smaller + 1): if((x % i == 0) and (y % i == 0)): hcf = i return hcf # ユーザーが2つの数字を入力 num1 = int(input("最初の数字を入力してください:")) num2 = int(input("2番目の数字を入力してください:")) print( num1,"と", num2,"の最大公約数は", hcf(num1, num2))

上記のコードを実行した出力結果は次のとおりです:

输入第一个数字: 54
输入第二个数字: 24
54 和 24 的最大公约数为 6

Document 对象参考手册Python3 実例

その他の拡張