Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/three.js/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在python中获得两个数字的因子?_Python - Fatal编程技术网

如何在python中获得两个数字的因子?

如何在python中获得两个数字的因子?,python,Python,代码的用途是什么: def gcd(x, y): (x, y) = (y, x) if x > y else (x, y) for factor in range(x, 0, -1): if x % factor == 0 and y % factor == 0: return factor 通过代码的其余部分获取因子还不够吗?我试图去掉第一行,但代码出错了,但我仍然无法理解它。这保证了结果x总是比y小 您需要了解GCD的算法。本质

代码的用途是什么:

def gcd(x, y):
    (x, y) = (y, x) if x > y else (x, y)
    for factor in range(x, 0, -1):
        if x % factor == 0 and y % factor == 0:
            return factor

通过代码的其余部分获取因子还不够吗?我试图去掉第一行,但代码出错了,但我仍然无法理解它。

这保证了结果x总是比y小

您需要了解GCD的算法。本质上,这条线是为了确保x小于y。
(x, y) = (y, x) if x > y else (x, y)