Python的math模塊中的gcd函數(shù)可以處理負(fù)數(shù)。在計(jì)算最大公約數(shù)時(shí),它會(huì)自動(dòng)將負(fù)數(shù)轉(zhuǎn)換為正數(shù)。如果你需要計(jì)算兩個(gè)負(fù)數(shù)的最大公約數(shù),只需將它們傳遞給gcd函數(shù),它會(huì)自動(dòng)處理。
例如:
import math
a = -12
b = -16
result = math.gcd(a, b)
print("The greatest common divisor of", a, "and", b, "is", result)
輸出結(jié)果將是:
The greatest common divisor of -12 and -16 is 4