Partition Numbers and a code to generate one in Python

Author: Kazi Abu Rousan

The pure mathematician, like the musician, is a free creator of his world of ordered beauty.

Bertrand Russell

Today we will be discussing one of the most fascinating idea of number theory, which is very simple to understand but very complex to get into. Today we will see how to find the Partition Number of any positive integer $n$.

Like every blog, our main focus will be to write a program to find the partition using python. But today we will use a very special thing, we will be using Math Inspector - A visual programming environment. It uses normal python code, but it can show you visual (block representation) of the code using something called Node Editor. Here is a video to show it's feature.

Beautiful isn't it? Let's start our discussion.

What are Partition Numbers?

In number theory, a Partition of a positive integer $n$, also called an Integer Partition, is a way of writing $n$ as a sum of positive integers (two sums that differ only in the order of their summands are considered the same partition, i.e., $2+4 = 4+ 2$). The partition number of $n$ is written as $p(n)$

As an example let's take the number $5$. It's partitions are;

$$5 = 5 +0\ \ \ \ \\= 4 + 1\ \ \ \ \\ = 3 + 2\ \ \ \ \\= 3 + 1+ 1\ \ \\= 2 +2 + 1\ \ \\= 2+1+1+1 \ \\= 1+1+1+1+1$$

Hence, $p(5) = 7$. Easy right?, Let's see partitions of all numbers from $0$ to $6$.

Note: The partition number of $0$ is taken as $1$.

Partition of numbers from $0$ to $6$.

See how easy it is not understand the meaning of this and you can very easily find the partitions of small numbers by hand. But what about big numbers?, like $p(200)$?

Finding the Partition Number

There is no simple formula for Partition Number. We normally use recursion but if you want just a formula which generate partition number of any integer $n$, then you have to use this horrifying formula:

Scary!!!-- Try using this to find $p(2)$

Just looking at this can give you nightmare. So, are there any other method?, well yes.

Although, there are not any closed form expression for the partition function up until now, but it has both asymptotic expansions(Ramanujan's work) that accurately approximate it and recurrence relations(euler's work) by which it can be calculated exactly.

Generating Function

One method can be to use a generating function. In simple terms we want a polynomial whose coefficient of $x^n$ will give us $p(n)$. It is actually quite easy to find the generating function.

$$ \sum_{n =0}^{\infty} p(n)x^n = \Pi_{j = 0}^{\infty}\sum_{i=0}^{\infty}x^{ji} = \Pi_{j = 1}^{\infty}\frac{1}{1-x^j}$$

This can also written as:

$$ \sum_{n =0}^{\infty} p(n)x^n = \frac{1}{(1-x)}\frac{1}{(1-x^2)}\frac{1}{(1-x^3)}\cdots = (1+x^1+x^2+\cdots+x^r+\cdots) (1+x^2+x^4+\cdots+x^{2r}+\cdots)\cdots $$

Each of these $(1+x^k+x^{2k}+\cdots+x^{rk}+\cdots)$ are called sub-series.

We can use this to find the partition number of any $n$. Let's see an example for $n=7$. There are 2 simple steps.

  1. First write the polynomial such that each sub-series's maximum power is smaller or equals to $n$. As an example, if $n=7$, then the polynomial is $(1+x+x^2+x^3+x^4+x^5+x^6+x^7)(1+x^2+x^4+x^6)(1+x^3+x^6)(1+x^4)(1+x^5)(1+x^6)(1+x^7)$.
  2. Now, we simplify this and find coefficient of $x^n$. In this process we find the partition numbers for all $r<n$.

So, for our example, we can just expand the polynomial given in point-1. You can do that by hand. But I have used sympy library.

from sympy import *
x = symbols('x')
a = expand((1+x+x**2+x**3+x**4+x**5+x**6+x**7)*(1+x**2+x**4+x**6)*(1+x**3+x**6)*(1+x**4)*(1+x**5)*(1+x**6)*(1+x**7))
print(a)
#Output: x**41 + x**40 + 2*x**39 + 3*x**38 + 5*x**37 + 7*x**36 + 11*x**35 + 15*x**34 + 18*x**33 + 24*x**32 + 30*x**31 + 37*x**30 + 44*x**29 + 52*x**28 + 57*x**27 + 64*x**26 + 71*x**25 + 77*x**24 + 81*x**23 + 84*x**22 + 84*x**21 + 84*x**20 + 84*x**19 + 81*x**18 + 77*x**17 + 71*x**16 + 64*x**15 + 57*x**14 + 52*x**13 + 44*x**12 + 37*x**11 + 30*x**10 + 24*x**9 + 18*x**8 + 15*x**7 + 11*x**6 + 7*x**5 + 5*x**4 + 3*x**3 + 2*x**2 + x + 1

The coefficients of $x^k$ gives $p(k)$ correctly up to $k = 7$. So, from this $p(7) = 15$, $p(6)=11$ and so on. But for $n>7$, the coefficients will be less than $p(n)$, as an example $p(8)=22$ but here the coefficient is $18$. To calculate $p(8)$, we need to take sub-series with power $8$ or less.

Although this method is nice, it is quite lengthy. But this method can be modified to generate a recurrence relation.

Euler's Pentagonal Number Theorem

Euler's Pentagonal Theorem gives us a relation to find the polynomial of the product of sub-series. The relation is:

$$ \Pi_{i = 1}^{\infty }(1-x^n) = 1 + \sum_{k =1}^{\infty } (-1)^k \Big(x^{k\frac{(3k+1)}{2}} + x^{k\frac{(3k-1)}{2}}\Big) $$

Using this equation, we get the recurrence relation,

$$p(n) = p(n-1) + p(n-2) - p(n-5) - p(n-7) + \cdots = \sum_{k=1}^{n} \Bigg( p\Bigg(n-\frac{k(3k-1)}{2}\Bigg) + p\Bigg(n-\frac{k(3k+1)}{2}\Bigg)\Bigg) $$

The numbers $0, 1, 2, 5, 7, 12, 15, 22, 26, 35, 40,\cdots $ , are called pentagonal numbers. We generate these numbers by $\frac{k(3k+1)}{2}$ and $\frac{k(3k-1)}{2}$.

Let's try to find $p(7)$, using this.

Using this simple algorithm, we can write a code to get partition number.

def par(n):
	summ = 0
	if n == 0 or n == 1:
		result = 1
	else:
		for k in range(1,n+1):
			d1 = n - int((k*(3*k-1))/2)
			d2 = n - int((k*(3*k+1))/2)
			sign = pow(-1,k+1)
			summ = summ + sign*(par(d1) + par(d2))
		result = summ
	return result

This is the code to generate partition number. This is not that efficient as it uses recurrence. For big numbers it will take huge amount of time. In math-inspector, we can see it's block diagram.

Here the par block represent the function par(n). Whenever i creates a variable $n1=6$ or $n3=10$, it creates a circle. To apply par on $n3$, i just need to connect their nodes. And to show the output, add the other node to output node.

So, we have seen this simple function. Now, let's define one using the euler's formula but in a different manner. To understand the used algorithm watch this: Explanation

def partition(n):
    odd_pos = []; even_pos= []; pos_d = []
    for i in range(1,n+1):
        even_pos.append(i)
        odd_pos.append(2*i+1)
    m = 0; k = 0
    for i in range(n-1):
        if i % 2 == 0:
            pos_d.append(even_pos[m])
            m += 1
        else:
            pos_d.append(odd_pos[k])
            k += 1

    initial = 1; pos_index = [initial]; count = 1
    for i in pos_d:
        d = initial + i
        pos_index.append(d)
        initial = d
        count += 1
        if count > n:
            break
    sign = []
    for i in range(n):
        if i % 4 == 2 or i % 4 == 3:
            sign.append(-1)
        else:
            sign.append(1)
    pos_sign = []; k = 0
    for i in range(1,n+1):
        if i in pos_index:
            ps = (i,sign[k])
            k = k + 1
            pos_sign.append(ps)
        else:
            pos_sign.append(0)
    if len(pos_sign) != n:
        print("Error in position and sign list.")
    partition = [1]
    f_pos = []
    for i in range(n):
        if pos_sign[i]:
            f_pos.append(pos_sign[i])
        partition.append(sum(partition[-p]*s for p,s in f_pos))
    return partition

This is the code. This is very efficient. Let's try running this function.

Note: This function returns a list which contains all $p(n)$ in the range $0$ to $n$.

As you can see in the program, In[3] gives a list $[p(0),p(1),p(2),p(3),p(4),p(5)]$. Hence, to get the $p(n)$ using this function, just add $[-1]$ as it gives the last element of the list.

We can use a simple program to plot the graph.

import matplotlib.pyplot as plt
n = 5
x_vals = [i for i in range(n+1)]
y_vals = partition(n)
plt.grid(color='purple',linestyle='--')
plt.ylabel("Partition Number")
plt.xlabel("Numbers")
plt.step(x_vals,y_vals,c="Black")
plt.savefig("Parti_5.png",bbox_inches = 'tight',dpi = 300)
plt.show()

Output is given below. The output is a step function as it should be.

This is all for today. I hope you have learnt something new. If you find it useful, then share.

Algebraic Equation | AIME I, 2000 Question 7

Try this beautiful problem from the American Invitational Mathematics Examination I, AIME I, 2000 based on Algebraic Equation.

Algebraic Equation - AIME 2000


Suppose that x,y and z are three positive numbers that satisfy the equation xyz=1, \(x+\frac{1}{z}=5\) and \(y+\frac{1}{x}=29\) then \(z+\frac{1}{y}\)=\(\frac{m}{n}\) where m and n are relatively prime, find m+n

  • is 107
  • is 5
  • is 840
  • cannot be determined from the given information

Key Concepts


Algebra

Equations

Integers

Check the Answer


Answer: is 5.

AIME, 2000, Question 7

Elementary Algebra by Hall and Knight

Try with Hints


 here \(x+\frac{1}{z}=5\) then1=z(5-x)=xyz putting xyz=1 gives 5-x=xy and \(y=(29-\frac{1}{x}\)) together gives 5-x=x\((29-\frac{1}{x}\)) then x=\(\frac{1}{5}\)

then y=29-5=24 and z=\(\frac{1}{5-x}\)=\(\frac{5}{24}\)

\(z+\frac{1}{y}\)=\(\frac{1}{4}\) then 1+4=5.

.

Subscribe to Cheenta at Youtube


Logarithms and Equations | AIME I, 2000 | Question 9

Try this beautiful problem from the American Invitational Mathematics Examination, AIME I, 2000 based on Logarithms and Equations.

Logarithms and Equations - AIME I 2000


\(log_{10}(2000xy)-log_{10}xlog_{10}y=4\) and \(log_{10}(2yz)-(log_{10}y)(log_{10}z)=1\) and \(log_{10}(zx)-(log_{10}z)(log_{10}x)=0\) has two solutions \((x_{1},y_{1},z_{1}) and (x_{2},y_{2},z_{2})\) find \(y_{1}+y_{2}\).

  • is 905
  • is 25
  • is 840
  • cannot be determined from the given information

Key Concepts


Logarithms

Theory of Equations

Number Theory

Check the Answer


Answer: is 25.

AIME I, 2000, Question 9

Polynomials by Barbeau

Try with Hints


Rearranging equations we get \(-logxlogy+logx+logy-1=3-log2000\) and \(-logylogz+logy+logz-1=-log2\) and \(-logxlogz+logx+logz-1=-1\)

taking p, q, r as logx, logy and logz, \((p-1)(q-1)=log2\) and \((q-1)(r-1)=log2\) and \( (p-1)(r-1)=1\) which is first system of equations and multiplying the first three equations of the first system gives \((p-1)^{2}(q-1)^{2}(r-1)^{2}=(log 2)^{2}\) gives \((p-1)(q-1)(r-1)=+-(log2)\) which is second equation

from both equations (q-1)=+-(log2) gives (logy)+-(log2)=1 gives \(y_{1}=20\),\(y_{2}=5\) then \(y_{1}+y_{2}=25\).

Subscribe to Cheenta at Youtube


Roots of Equation and Vieta's formula | AIME I, 1996 Problem 5

Try this beautiful problem from the American Invitational Mathematics Examination, AIME, 1996 based on Roots of Equation and Vieta's formula.

Roots of Equation and Vieta's formula - AIME I, 1996


Suppose that the roots of \(x^{3}+3x^{2}+4x-11=0\) are a,b and c and that the roots of \(x^{3}+rx^{2}+sx+t=0\) are a+b,b+c and c+a, find t.

  • is 107
  • is 23
  • is 840
  • cannot be determined from the given information

Key Concepts


Functions

Roots of Equation

Vieta s formula

Check the Answer


Answer: is 23.

AIME I, 1996, Question 5

Polynomials by Barbeau

Try with Hints


With Vieta s formula

\(f(x)=x^{3}+3x^{2}+4x-11=(x-a)(x-b)(x-c)=0\)

\(\Rightarrow a+b+c=-3\), \(ab+bc+ca=4\) and \(abc=11\)

Let a+b+c=-3=p

here t=-(a+b)(b+c)(c+a)

\(\Rightarrow t=-(p-c)(p-a)(p-b)\)

\(\Rightarrow t=-f(p)=-f(-3)\)

\(t=-[(-3)^{3}+3(-3)^{2}+4(-3)-11]\)

=23.

Subscribe to Cheenta at Youtube


Roots and coefficients of equations | PRMO 2017 | Question 4

Try this beautiful problem from the PRMO, 2017 based on Roots and coefficients of equations.

Roots and coefficients of equations - PRMO 2017


Let a,b be integers such that all the roots of the equation \((x^{2}+ax+20)(x^{2}+17x+b)\)=0 are negetive integers, find the smallest possible values of a+b.

  • is 107
  • is 25
  • is 840
  • cannot be determined from the given information

Key Concepts


Polynomials

Roots

Coefficients

Check the Answer


Answer: is 25.

PRMO, 2017, Question 4

Polynomials by Barbeau

Try with Hints


\((x^{2}+ax+20)(x^{2}+17x+b)\)

where sum of roots \( \lt \) 0 and product \( \gt 0\) for each quadratic equation \(x^{2}\)+ax+20=0 and

\((x^{2}+17x+b)=0\)

\(a \gt 0\), \(b \gt 0\)

now using vieta's formula on each quadratic equation \(x^{2}\)+ax+20=0 and \((x^{2}+17x+b)=0\), to get possible roots of \(x^{2}\)+ax+20=0 from product of roots equation \(20=(1 \times 20), (2 \times 10), (4 \times 5)\)

min a=4+5=9 from all sum of roots possible

again using vieta's formula, to get possible roots of \((x^{2}\)+17x+b)=0 from sum of roots equation \(17=-(\alpha + \beta) \Rightarrow (\alpha,\beta)=(-1,-16),(-2,-15),\)

\((-8,-9)\)

min b=(-1)(-16)=16 from all products of roots possible

\((a+b)_{min}=a_{min}+b_{min}\)=9+16=25.

Subscribe to Cheenta at Youtube


Roots of Equation | PRMO 2017 | Question 19

Try this beautiful problem from the Pre-RMO, 2017 based on roots of equation.

Roots of equation - PRMO 2017


Suppose 1,2,3 are roots of the equation \(x^{4}+ax^{2}+bx=c\). Find the value of c.

  • is 107
  • is 36
  • is 840
  • cannot be determined from the given information

Key Concepts


Roots

Equations

Algebra

Check the Answer


Answer: is 36.

PRMO, 2017, Question 19

Higher Algebra by Hall and Knight

Try with Hints


1,2,3 are the roots of \(x^{4}+ax^{2}+bx-c=0\)

since sum of roots=0 fourth root=-6 by using Vieta's formula

c=36.

Subscribe to Cheenta at Youtube


Solving Equation | PRMO 2017 | Question 23

Try this beautiful problem from the Pre-RMO, 2017, Question 23, based on Solving Equation.

Solving Equation - PRMO 2017, Question 23


Suppose an integer r, a natural number n and a prime number p satisfy the equation \(7x^{2}-44x+12=p^{n}\). Find the largest value of p.

  • is 107
  • is 47
  • is 840
  • cannot be determined from the given information

Key Concepts


Equation

Algebra

Integers

Check the Answer


Answer: is 47.

PRMO, 2017, Question 23

Higher Algebra by Hall and Knight

Try with Hints


\(7x^{2}-44x+12=p^{n}\)

or, \(7x^{2}-42x-2x+12=p^{n}\)

or, \((7x-2)(x-6)=p^{n}\)

or, \(7x-2=p^{\alpha}\), \(x-6=p^{\beta}\)

or, \((7x-2)-7(x-6)\)=\(p^{\alpha}-7p^{\beta}\)

\(40=p^{\alpha}-7p^{\beta}\)

If \({\alpha}, {\beta}\) are natural numbers, p is a divisor of 40

or, p=2 or 5

If p=2, 40=\(2^{\alpha}-7(2^{\beta})\) or, \((2^{3})(5)\)=\(2^{\alpha}-7(2^{\beta})\)

or, \({\beta}\)=3 and \(2^{\alpha}\)=40+56

or, \({\alpha}\) not an integer hence not possible

If p=5 then 40=\(5^{\alpha}-7(5^{\beta})\)

or, \((2)^{3}(5)=5^{\alpha}-7(5^{\beta})\)

or, \({\beta}=1\) and \(5^{\alpha}\)=40+35

or, \({\alpha}\) not an integer hence not possible

so \({\beta}\)=0 or, \(p^{\alpha}\)=47

or, p=47 and \({\alpha}\)=1.

Subscribe to Cheenta at Youtube


Positive solution | AIME I, 1990 | Question 4

Try this beautiful problem from the American Invitational Mathematics Examination I, AIME I, 1990 based on positive solution.

Positive solution - AIME I, 1990


Find the positive solution to

\(\frac{1}{x^{2}-10x-29}+\frac{1}{x^{2}-10x-45}-\frac{2}{x^{2}-10x-69}=0\)

  • is 107
  • is 13
  • is 840
  • cannot be determined from the given information

Key Concepts


Integers

Divisibility

Algebra

Check the Answer


Answer: is 13.

AIME I, 1990, Question 4

Elementary Algebra by Hall and Knight

Try with Hints


here we put \(x^{2}-10x-29=p\)

\(\frac{1}{p}+\frac{1}{p-16}-\frac{2}{p-40}=0\)

or, (p-16)(p-40)+p(p-40)-2p(p-16)=0

or, -64p+(40)(16)=0

or, p=10

or, 10=\(x^{2}-10x-29\)

or, (x-13)(x+3)=0

or, x=13 positive solution.

Subscribe to Cheenta at Youtube


Number of roots Problem | TOMATO B.Stat Objective 712

Try this problem from I.S.I. B.Stat Entrance Objective Problem based on Number of roots.

Number of roots - B.Stat Objective Problem


The number of roots of the equation \(xsinx=1\) in the interval \([0,{2\pi}]\) is

  • 0
  • 2
  • 53361
  • 5082

Key Concepts


Equation

Roots

Algebra

Check the Answer


Answer:2

B.Stat Objective Problem 712

Challenges and Thrills of Pre-College Mathematics by University Press

Try with Hints


\(f(x)=xsinx-1=0\)

which can be written as sinx=\(\frac{1}{x}\) and f(x) has solution at those points where sinx and \(\frac{1}{x}\) intersects

So let us draw the graph

here we see two graphs y=sin x and y=\(\frac{1}{x}\)

Number of roots - graph

both graphs intersect at two points between \((0,2\pi]\)

or, number of roots is 2.

Subscribe to Cheenta at Youtube


Roots of Equation | TOMATO B.Stat Objective 711

Try this problem from I.S.I. B.Stat Entrance Objective Problem based on Roots of Equation.

Roots of Equations (B.Stat Objective Question )


The number of roots of the equation \(x^2+sin^2{x}-1\) in the closed interval \([0,\frac{\pi}{2}]\) is

  • 0
  • 2
  • 53361
  • 5082

Key Concepts


Equation

Roots

Algebra

Check the Answer


Answer:2

B.Stat Objective Problem 711

Challenges and Thrills of Pre-College Mathematics by University Press

Try with Hints


\(x^2+sin^2{x}-1=0\)

\(\Rightarrow x^{2}=cos^{2}x\)

we draw two graphs \(y=x^{2} and y=cos^{2}x\)

where intersecting point gives solution now we look for intersecting points

we get two intersecting points

so number of roots is 2.

Subscribe to Cheenta at Youtube