带有Python示例的math.gcd()方法

Python math.gcd()方法 (Python math.gcd() method)

math.gcd() method is a library method of math module, it is used to find GCD (Greatest Common Divisor) of given numbers, it accepts two integer numbers and returns their greatest common divisor (the largest positive integer that divides both of the numbers).

math.gcd()方法数学模块的库方法,用于查找给定数字的GCD(最大公约数),它接受两个整数并返回其最大公约数(将两个整数相除的最大正整数)。数字)。

Note:

注意:

  • math.gcd() is available in newer version Python 3.5

    math.gcd()在更高版本的Python 3.5中可用

  • If both numbers are 0 – it returns 0.

    如果两个数字均为0 –则返回0。

  • If one of the numbers is 0 – it returns another non-zero number.

    如果其中一个数字为0,则返回另一个非零数字。

  • If anything else like float or string is provided, the method returns a "TypeError",

    如果提供了其他类似float或string的方法,则该方法将返回“ TypeError”

    In the case of float parameter, it returns

    对于float参数,它返回

    "TypeError: 'float' object cannot be interpreted as an integer"

    “ TypeError:'float'对象无法解释为整数”

    In the case of a string parameter, it returns

    如果是字符串参数,则返回

    "TypeError: 'str' object cannot be interpreted as an integer"

    “ TypeError:'str'对象无法解释为整数”

Syntax of math.gcd() method:

math.gcd()方法的语法:

    math.gcd(a, b)

Parameter(s): a, b – two integer numbers whose greatest common divisor has to be calculated.

参数: a,b –两个整数,必须计算其最大公约数。

Return value: int – it returns an integer value, which is the GCD (Greatest Common Divisor) i.e. the largest integer number that divided both numbers.

返回值: int –它返回一个整数值,即GCD(最大公约数),即将两个数字相除的最大整数。

Example:

例:

    Input:
    a = 10
    b = 15

    # function call
    print(math.gcd(a, b))

    Output:
    15

Python代码演示math.gcd()方法的示例 (Python code to demonstrate example of math.gcd() method)

# python code to demonstrate example of 
# math.gcd() method

# importing math module
import math

# numbers
a = 10
b = 15
print("gcd is = ", math.gcd(a,b))

a = 10
b = 0
print("gcd is = ", math.gcd(a,b))

a = 0
b = 0
print("gcd is = ", math.gcd(a,b))

Output

输出量

gcd is =  5
gcd is =  10
gcd is =  0


翻译自: https://www.includehelp.com/python/math-gcd-method-with-example.aspx

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值