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
p0=1pn=sum$[p(n-x)|x<-[n,(n-1)..0]]
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
importData.Arraypartition::Int->Integerpartitionn=partition'!(n,n)wherepartition'=listArray((0,0),(n,n))[pmk|m<-[0..n],k<-[0..n]]p_0=0p0_=1pmk=sum[partition'!((m-i),i)|i<-[0..(minmk)]]--this is the dp step
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
f0=[1,0,0,0,0,0,0,0...]f1=[1,1,1,1,1,1,1,1...]f2=[1,1,2,2,3,3,4,4...]f3=[1,1,2,3,4,5,7,8...]\--and in general,f(k+1)==zipWith(+)(fk)(replicate(k+1)0++f(k+1))
Notice that the first k+1 terms of fk+1 can be inherited from those of fk, 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
This motivates us to exploit the lazyness of haskell and write fk+1 in terms of g, which, by virtue of laziness, has been calculated to a certain extent, already.
1
f(k+1)=take(k+1)g++drop(k+1)(f(k+1))
Now, we have adequate background to implement g itself:
1
2
3
4
5
g=go1(1:repeat0)wheregok(x:xs)=x:go(k+1)ys--think of xs as drop k (f (k-1) )whereys=zipWith(+)xs(takekfinal++ys)--think of ys as drop k (f k)
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
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:
Use the emojis to react to an explanation, whether you're congratulating a job well done , or just really confused .
Ask specific questions about the challenge or the steps in somebody's explanation. Well-posed questions can add a lot to the discussion, but posting "I don't understand!" doesn't help anyone.
Try to contribute something new to the discussion, whether it is an extension, generalization or other idea related to the challenge.
Stay on topic — we're all here to learn more about math and science, not to hear about your favorite get-rich-quick scheme or current world events.
Markdown
Appears as
*italics* or _italics_
italics
**bold** or __bold__
bold
- bulleted - list
bulleted
list
1. numbered 2. list
numbered
list
Note: you must add a full line of space before and after lists for them to show up correctly
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.
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_{i-1}
\frac{2}{3}
\sqrt{2}
\sum_{i=1}^3
\sin \theta
\boxed{123}
Comments
i 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
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 :)
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