📜  Python中的 divmod() 及其应用

📅  最后修改于: 2022-05-13 01:55:21.244000             🧑  作者: Mango

Python中的 divmod() 及其应用

Python中的 divmod() 方法接受两个数字并返回一对由它们的商和余数组成的数字。

句法 :

divmod(x, y)
x and y : x is numerator and y is denominator
x and y must be non complex

例子:

Input : x = 9, y = 3
Output :(3, 0)

Input : x = 8, y = 3
Output :(2, 2)

说明: divmod() 方法有两个参数 x 和 y,其中 x 被视为分子,y 被视为分母。该方法计算x // yx % y并返回这两个值。

  • 如果 x 和 y 是整数,则返回值为
(x // y, x % y)
  • 如果 x 或 y 是浮点数,则结果为
(q, x % y), where q is the whole part of the quotient.
Python3
# Python3 code to illustrate divmod()
# divmod() with int
print('(5, 4) = ', divmod(5, 4))
print('(10, 16) = ', divmod(10, 16))
print('(11, 11) = ', divmod(11, 11))
print('(15, 13) = ', divmod(15, 13))
 
# divmod() with int and Floats
print('(8.0, 3) = ', divmod(8.0, 3))
print('(3, 8.0) = ', divmod(3, 8.0))
print('(7.5, 2.5) = ', divmod(7.5, 2.5))
print('(2.6, 10.7) = ', divmod(2.6, 0.5))


PYTHON3
# Python code to find if a number is
# prime or not using divmod()
 
# Given integer
n = 15
x = n
 
# Initialising counter to 0
count = 0
while x != 0:
    p, q = divmod(n, x)
    x -= 1
    if q == 0:
        count += 1
if count > 2:
    print('Not Prime')
else:
    print('Prime')


Python3
# Sum of digits of a number using divmod
num = 86
sums = 0
while num != 0:
    use = divmod(num, 10)
    dig = use[1]
    sums = sums + dig
    num = use[0]
print(sums)


Python3
# reversing a number using divmod
num = 132
pal = 0
while num != 0:
    use = divmod(num, 10)
    dig = use[1]
    pal = pal*10+dig
    num = use[0]
print(pal)


输出:

(5, 4) =  (1, 1)
(10, 16) =  (0, 10)
(11, 11) =  (1, 0)
(15, 13) =  (1, 2)
(6.0, 5) =  (2.0, 2.0)
(3, 9.0) =  (0.0, 3.0)
(13.5, 6.2) =  (3.0, 0.0)
(1.6, 10.7) =  (5.0, 0.10000000000000009)

错误和异常

  1. 如果任一参数(例如 x 和 y)是浮点数,则结果为 (q, x%y)。这里,q 是商的整个部分。
  2. 如果第二个参数为 0,则返回零除法错误
  3. 如果第一个参数为 0,则返回 (0, 0)

实际应用:使用 divmod()函数检查数字是否为素数。

例子:

Input : n = 7
Output :Prime

Input : n = 15
Output :Not Prime

算法

  1. 初始化一个新变量,比如 x 用给定的整数和一个变量计数器为 0
  2. 运行一个循环,直到给定的整数变为 0 并继续递减它。
  3. 将 divmod(n, x) 返回的值保存在两个变量中,例如 p 和 q
  4. 检查 q 是否为 0,这意味着 n 可以完全被 x 整除,因此递增计数器值
  5. 检查计数器值是否大于2,如果是,则该数不是素数,否则为素数

Python3

# Python code to find if a number is
# prime or not using divmod()
 
# Given integer
n = 15
x = n
 
# Initialising counter to 0
count = 0
while x != 0:
    p, q = divmod(n, x)
    x -= 1
    if q == 0:
        count += 1
if count > 2:
    print('Not Prime')
else:
    print('Prime')

输出:

Not Prime

更多应用:

示例 1:

Python3

# Sum of digits of a number using divmod
num = 86
sums = 0
while num != 0:
    use = divmod(num, 10)
    dig = use[1]
    sums = sums + dig
    num = use[0]
print(sums)

输出:

14

示例 2:

Python3

# reversing a number using divmod
num = 132
pal = 0
while num != 0:
    use = divmod(num, 10)
    dig = use[1]
    pal = pal*10+dig
    num = use[0]
print(pal)

输出:

231