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 

Easy Math Editor
This discussion board is a place to discuss our Daily Challenges and the math and science related to those challenges. Explanations are more than just a solution — they should explain the steps and thinking strategies that you used to obtain the solution. Comments should further the discussion of math and science.
When posting on Brilliant:
*italics*
or_italics_
**bold**
or__bold__
paragraph 1
paragraph 2
[example link](https://brilliant.org)
> This is a quote
\(
...\)
or\[
...\]
to ensure proper formatting.2 \times 3
2^{34}
a_{i1}
\frac{2}{3}
\sqrt{2}
\sum_{i=1}^3
\sin \theta
\boxed{123}
Comments
Sort by:
Top Newesti don't understand programming very well, but i can see your hard work . Keep it up :) .
Log in to reply
Thanks!
Let's change that fact.
Log in to reply
cool
Log in to reply
this is really cool ! I want to know more
Log in to reply
The Wikipedia page is a good place to start.
Log in to reply
ok thanks I appreaciate it :)
Log in to reply
Can you please elaborate on the dynamic approach? Can you provide other links which can help me understand such algorithms in Haskell better? Thank you.
Log in to reply
You can check out this wiki I wrote to understand dynamic programming
Log in to reply