python中math库中的gcd 您所在的位置:网站首页 python中求最大公约数和最小公倍数函数 python中math库中的gcd

python中math库中的gcd

2023-08-18 22:53| 来源: 网络整理| 查看: 265

在Python中,数学模块包含许多数学运算,可以使用该模块轻松执行。math.gcd()函数计算其参数中提到的2个数的最大公约数。

用法: math.gcd(x, y)

参数:

x:必须计算其gcd的非负整数。

y:必须计算其gcd的非负整数。

返回:计算给定参数x和y的GCD后的绝对/正整数值。

异常:当x和y均为0时,函数返回0,如果任何数字为字符,则会引发Type错误。

代码1:

# Python code to demonstrate the working of gcd()

# importing "math" for mathematical operations

import math

# prints 12

print ("The gcd of 60 and 48 is:", end ="")

print (math.gcd(60, 48))

输出:

The gcd of 60 and 48 is:12

代码2:

# Python code to demonstrate the working of gcd()

# importing "math" for mathematical operations

import math

# prints gcd of x, y

print ("math.gcd(44, 12):", math.gcd(44, 12))

print ("math.gcd(69, 23):", math.g



【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有