Problem (Integer Partitions): Given an integer \(n\), in how many ways can the integer be expressed as a sum of integers?
Take I: Dynamic Programming
Let's name the function we want to compute \(p(n)\). Our first stab would be to think of something like this:
1 2 

However, this is not quite correct. We're overcounting the partitions. (Think why)
To avoid overcounting, we instead ask: Given two integers \(n\) and \(k\), in how many ways can \(n\) be partitioned using integers less than or equal to \(k\)?
1 2 3 4 5 6 7 8 9 

Take II: Generating Functions
(Influenced by Math.Combinat)
It is not hard to see that what we want is the coefficient of \(n\) in \(g(x) = \prod_{k}\frac{1}{1x^k}\).
Now, consider the following sequence of power series \(f_n(x)\) converging to \(g(x)\).
\[ \frac{1}{(1x)} = 1 + x + x^2 + x^3 + x^4 + x^5 + x^6 + x^7 + + \cdots \\ \frac{1}{(1x)(1x^2)} = 1 + x + 2x^2 + 2x^3 + 3x^4 + 3x^5 + 4x^6 + 4x^7 + \cdots \\ \frac{1}{(1x)(1x^2)(1x^3)} = 1 + x + 2x^2 + 3x^3 + 4x^4 + 5x^5 + 7x^6 + 8x^7 + \cdots \]
In general, \(f_{n+1} = f_n + x^{n+1}f_{n+1}\).
At this point, let us switch to the infinite list representations of the power series. We have:
1 2 3 4 5 6 7 8 

Notice that the first \(k+1\) terms of \(f_{k+1}\) can be inherited from those of \(f_k\), and are the same as those of \(g\). To express the same idea in Haskell, we drop the first \(k+1\) terms in the power series and get
1 

This motivates us to exploit the lazyness of haskell and write \(f_{k+1}\) in terms of \(g\), which, by virtue of laziness, has been calculated to a certain extent, already.
1 

Now, we have adequate background to implement g itself:
1 2 3 4 5 

It's worth appreceating that the gf solution works really fast as compared to the dynamic programming solution.
Take III: Pentagonal Number Theorem
(I'm hoping someone would explain this better in the comments section.)
Euler's Pentagonal Number Theorem suggests that the denominator of the generating function discussed above could be written as
\[ (1x)(1x^2)(1x^3)\cdots = 1  x  x^2 + x^5 + x^7  x^{12}  x^{15} + \cdots \]
The numbers used here are Generalized Pentagonal Numbers.
It can be deduced that \[p(k) = p(k − 1) + p(k − 2) − p(k − 5) − p(k − 7) + p(k − 12) + p(k − 15) − p(k − 22) − ...\] where the numbers used are generalized Pentagonal Numbers.
1 2 3 4 5 6 7 

Comments
Sort by:
Top Newesti don't understand programming very well, but i can see your hard work . Keep it up :) . – Sambhrant Sachan · 2 months, 3 weeks ago
Log in to reply
Let's change that fact. – Agnishom Chattopadhyay · 2 months, 3 weeks ago
Log in to reply
– Charlotte Milanese · 2 months ago
coolLog in to reply
this is really cool ! I want to know more – Charlotte Milanese · 2 months ago
Log in to reply
Wikipedia page is a good place to start. – Agnishom Chattopadhyay · 2 months ago
TheLog in to reply
– Charlotte Milanese · 2 months ago
ok thanks I appreaciate it :)Log in to reply