Python| sympy.divisor_sigma() 方法
借助sympy.divisor_sigma()方法,我们可以找到除数函数对于正整数n 。 divisor_sigma(n, k)等于n的所有除数的总和,以k或sum([x**k for x in divisors(n)])的幂次幂计算。
Syntax: divisor_sigma(n, k)
Parameter:
n – It denotes an integer.
k – It denotes an integer(optional). Default for k is 1.
Returns: Returns the sum of all the divisors of n raised to the power of k.
示例 #1:
# import divisor_sigma() method from sympy
from sympy.ntheory import divisor_sigma
n = 8
# Use divisor_sigma() method
divisor_sigma_n = divisor_sigma(n)
print("divisor_sigma({}) = {} ".format(n, divisor_sigma_n))
# 1 ^ 1 + 2 ^ 1 + 4 ^ 1 + 8 ^ 1 = 15
输出:
divisor_sigma(8) = 15
示例 #2:
# import divisor_sigma() method from sympy
from sympy.ntheory import divisor_sigma
n = 15
k = 2
# Use divisor_sigma() method
divisor_sigma_n = divisor_sigma(n, k)
print("divisor_sigma({}, {}) = {} ".format(n, k, divisor_sigma_n))
# 1 ^ 2 + 3 ^ 2 + 5 ^ 2 + 15 ^ 2 = 260
输出:
divisor_sigma(15, 2) = 260