Python | sympy.partition() method

With the help of sympy.partition() method, we can find Partition number in SymPy.

partition(n) -


The Partition numbers are a sequence of integers pn that represent the number of distinct ways of representing n as a sum of natural numbers (with order irrelevant). The generating function for pn is given by –
\sum_{n=0}^\infty p_n x^n = \prod_{k=1}^\infty (1 - x^k)^{-1}.



Syntax: partition(n)

Parameter:
n – It denotes the number upto which partition number is to be calculated.

Returns: Returns the nth partition number.

Example #1:

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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))  

chevron_right


Output:

Value of n = 7
Value of nth partition number : 15

Example #2:

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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))  

chevron_right


Output:

Value of n = 10
N partition number are : [1, 2, 3, 5, 7, 11, 15, 22, 30, 42]


My Personal Notes arrow_drop_up

Coder Machine Learner Social Activist Vocalist

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.