温馨提示×

python的gcd函数是否可以处理负数

小樊
82
2024-09-10 15:23:48
栏目: 编程语言

Python的math模块中的gcd函数可以处理负数。在计算最大公约数时,它会自动将负数转换为正数。如果你需要计算两个负数的最大公约数,只需将它们传递给gcd函数,它会自动处理。

例如:

import math

a = -12
b = -16

result = math.gcd(a, b)
print("The greatest common divisor of", a, "and", b, "is", result)

输出结果将是:

The greatest common divisor of -12 and -16 is 4

0