Python sympy.divisor_sigma()方法
借助sympy.divisor_sigma()方法,我们可以找到正整数n的除数函数。
语法: divisor_sigma(n, k)
参数 :
n –它表示一个整数。
k –它表示一个整数(可选)。k的默认值是1。
返回:返回n的所有除数升至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