Python| sympy.partition() 方法
借助sympy.partition()方法,我们可以在 SymPy 中找到分区号。
分区(n) -
分区数是整数序列p n ,表示将n表示为自然数之和的不同方式的数量(顺序无关)。 p n的生成函数由下式给出 -
.
Syntax: partition(n)
Parameter:
n – It denotes the number upto which partition number is to be calculated.
Returns: Returns the nth partition number.
示例 #1:
# import sympy
from sympy import *
n = 7
print("Value of n = {}".format(n))
# Use sympy.partition() method
nth_partition = partition(n)
print("Value of nth partition number : {}".format(nth_partition))
输出:
Value of n = 7
Value of nth partition number : 15
示例 #2:
# import sympy
from sympy import *
n = 10
print("Value of n = {}".format(n))
# Use sympy.partition() method
n_partition = [partition(x) for x in range(1, 11)]
print("N partition number are : {}".format(n_partition))
输出:
Value of n = 10
N partition number are : [1, 2, 3, 5, 7, 11, 15, 22, 30, 42]