Python v3.1の分数モジュールを使用して、最大公約数を計算しています。どのアルゴリズムが使われているのか知りたいのですが。ユークリッド法を推測していますが、確かにしたいと思います。ドキュメント(http://docs.python.org/py3k/library/fractions.html?highlight=fractions.gcd#fractions.gcd)は役に立ちません。誰かが私を手がかりにできますか?
6637 次
2 に答える
23
オンラインの3.1.2ソースコードによると、これは次のgcd
ように定義されていPython-3.1.2/Lib/fractions.py
ます。
def gcd(a, b):
"""Calculate the Greatest Common Divisor of a and b.
Unless b==0, the result will have the same sign as b (so that when
b is divided by it, the result comes out positive).
"""
while b:
a, b = b, a%b
return a
そうです、それは純粋なPythonで書かれたユークリッドの互除法です。
于 2010-06-03T00:53:46.277 に答える
3
于 2019-02-05T07:12:10.817 に答える