5

lets there is a number (N) and partitions (p), I have all the possible combinations, each combination sums to N. I want to calculate the nth term of these sequences/ combinations given N and p.

N = 7, p = 3

1: 1,1,5
2: 1,2,4
3: 1,3,3
4: 1,4,2
5: 1,5,1

6: 2,1,4 7: 2,2,3 8: 2,3,2 9: 2,4,1

10: 3,1,3 11: 3,2,2 12: 3,3,1

13: 4,1,2 14: 4,2,1

15: 5,1,1

N = 7, p = 4

1: 1,1,1,4
2: 1,1,2,3
3: 1,1,3,2
4: 1,1,4,1

5: 1,2,1,3 6: 1,2,2,2 7: 1,2,3,1

8: 1,3,1,2 9: 1,3,2,1

10: 1,4,1,1

11: 2,1,1,3 12: 2,1,2,2 13: 2,1,3,1

14: 2,2,1,2 15: 2,2,2,1

16: 2,3,1,1

17: 3,1,1,2 18: 3,1,2,1

19: 3,2,1,1

20: 4,1,1,1

N = 7, p = 5

1: 1,1,1,1,3
2: 1,1,1,2,2
3: 1,1,1,3,1

4: 1,1,2,1,2 5: 1,1,2,2,1

6: 1,1,3,1,1

7: 1,2,1,1,2 8: 1,2,1,2,1

9: 1,2,2,1,1

10: 1,3,1,1,1

11: 2,1,1,1,2 12: 2,1,1,2,1

13: 2,1,2,1,1

14: 2,2,1,1,1

15: 3,1,1,1,1

Milten
  • 7,722
  • Strictly speaking those are compositions rather than partitions or compositions. You can identify the $n$th composition slowly by stripping out binomial coefficients. – Henry Aug 18 '21 at 10:52
  • 1
    I would advice you to make a more descriptive title. Keywords: $k$-compositions, lexicographical order. Note that partition is not the right word, because that is used when the order of summands is ignored. – Milten Aug 18 '21 at 10:53
  • For example, in your $N=7$ with $p=4$ parts example, and looking for the $n=18$th case, there are ${7-1 \choose 4-1}=20$ possibilities of which ${7-1-1 \choose 3-1}=10$ start with $1$ and ${7-2-1 \choose 3-1}=6$ start with $2$ and ${7-3-1 \choose 3-1}=3$ start with $3$ and since $10+6 \lt 18 \le 10+6+3$ we can see the first part is $3$. Then we are faced with the subproblem of $N=7-3=4$ and $p=4-1=3$ and looking for the $n=18-(10+6)=2$nd term, and a similar exercise will tell you the second part is $1$. Next the subproblem is $N=4-1=3$ and $p=3-1=2$ looking for the $n=2-0=2$nd term... – Henry Aug 18 '21 at 11:42

1 Answers1

4

The way you divided up your lists already speaks to a way of doing it. Let $a_1, \ldots, a_n$ denote the elements of the answer. The number of compositions starting with $k$ is $\binom{N-k-1}{p-2}$. So compute partial sums $$ s_m(N,p) = \sum_{k=1}^m \binom{N-k-1}{p-2}, $$ until $s_m \ge n$. Set $$ a_1 = F(n,N,p) := \min\{m \mid s_m\ge n\}. $$ Now repeat with $$ a_{i+1} = F(n-s_{a_i-1}, N-a_i, p-1) $$ for $i\in\{1, \ldots, p-2\}$. $a_n$ will be a slightly special (and easy) case.


EDIT:

I realized that we have the closed formula $$ s_m(N,p) = \binom{N-1}{p-1} - \binom{N-1-m}{p-1}, $$ which follows from the identity $\sum_{j=k}^n\binom jk = \binom{n+1}{k+1}$. With this, we can actually do a binary search for $m$ to speed things up.


Let's say $N=7$, $p=4$, $n=5$. Compute $$ s_1 = \binom52 = 10 \ge 5. $$ So $a_1 = 1$. Repeat with $N=6$, $p=3$, $n=5$: $$ \begin{split} s_1 &= \binom41 = 4 < 5, \\ s_2 &= s_1 + \binom31 = 7 \ge 5, \end{split}$$ So $a_2 = 2$. Repeat with $N=4$, $p=2$, $n=1$: $$ s_1 = \binom20 = 1 \ge 1 $$ So $a_3 = 1$. Repeat with $N=3$, $p=1$, $n=1$. Clearly $a_4=3$ now. So the answer is $(1,2,1,3)$.

Milten
  • 7,722
  • Alternatively, you can search from the bottom of list instead of the top by substracting binomial coefficients instead of adding. This will speed things up a bit if $n$ is large. – Milten Aug 18 '21 at 11:24
  • thanks for the answer. can you please add some steps so I can dry run: let's say for N= 7, p = 4, and 5th term. I have to write a computer program for that. – Tasawar Hussain Aug 18 '21 at 11:40
  • @TasawarHussain I added an example, it should be pretty clear now. Note that the case $p=1$ is gonna be a special case in the code – I'll let you figure that out. Note also that you could just generate the list from the beginning and figure out the rules for how to go to the next element. Wouldn't be that must slower. If this is a coding assignment, that might well have been the intended solution. – Milten Aug 18 '21 at 12:20
  • Milten thanks for the example, yes it is clear now. I have already written code to generate all these sequences and this approach is similar to that. and yes it will be slow, as its sequential. – Tasawar Hussain Aug 18 '21 at 12:30
  • Actually, for large values of $N$, you should save quite a bit of time when I think about it. Generating the full list takes $O(N^p)$ time. With this method instead, you could find all relevant binomial coefficients in $O(N^2)$ time and the recursion is $O(Np)$ I think, so everything should be $O(N(N+p))$. This doens't take into account the value of $n$ though – for small values the list might be faster in order to avoid calculating coeffecients. – Milten Aug 18 '21 at 12:44
  • Yes, my current approach is (^p), will try to implement this as well! – Tasawar Hussain Aug 18 '21 at 12:49
  • Can you please add a bit more on how n has changed from 5 -> 5 -> 1 – Tasawar Hussain Aug 18 '21 at 15:30
  • 1
    You subtract the last sum that was $<n$. For example $n=5-4=1$ because $s_1$ was $4$. – Milten Aug 18 '21 at 15:36