3

I was attending a coding competition and this question came up

There is a box containing n chocolates. You can either take 1 chocolate or 3 chocolates at a time until its empty. So in total how many ways you can empty the chocolate for a given n.

Eg: n = 3

ways you can take out is 2 ie:

111
3

ways you can take out when n=4 is 3 ie:

1111
31
13

likewise, I have calculated manually for n=5 -> 4 etc etc..

so the result sequence comes up like 2,3,4,6,9...

I am not able to figure out what kind of relation it has or how do I solve it. I'm not able to figure out what kind of problem it is as well (Permutation, combinations, series?). let me know if I need to post any more details.

YuiTo Cheng
  • 4,705
Shobi
  • 133
  • So you're looking at the number of positive integers solutions to $$ 3x + y = k $$ for a fixed value for $k$. Maybe something like this can help: https://math.stackexchange.com/questions/29019/integral-solutions-of-linear-equation and https://www.handakafunda.com/how-to-solve-integral-solutions-questions-for-cat/ – Matti P. Jan 31 '19 at 10:27
  • 2
    @MattiP. I think $x,y$ should be non-negative integers, not be positive integers. And the order is also important. For example, if $k=4$, then $(x,y)=(1,1)$ or $(0,4)$. But in the case $(x,y)=(1,1)$, $31$ and $13$ is not the same way... – with-forest Jan 31 '19 at 10:33
  • yes. the order is important. – Shobi Jan 31 '19 at 10:37
  • The OEIS is always available as a last (or first) resort. Entering your sequence 2, 3, 4, 6, 9 gives A000930 which includes lots of (potentially) useful stuff. The Formulas section may be of use to you. – Peter Phipps Jan 31 '19 at 14:13
  • Thanks, Peter. I actually tried some online sequence solutions, but they didn't gave me much of idea. but this one I am encountering it for the first time its super usefull – Shobi Feb 01 '19 at 06:01

2 Answers2

6

As we can take one or three, we have the recurrence

$$c_n=c_{n-1}+c_{n-3}$$

with the initial condition

$$c_2=c_1=c_0=1,$$ as with $n<3$ there is a single option.

It is an easy matter to program this using a recursive function, preferably with memoization. Anon-recursive solution is also possible and preferable, storing the values in an array.

C= [1, 1, 1]
for n in range(3, 20):
    C.append(C[n-1] + C[n-3])

1, 1, 1, 2, 3, 4, 6, 9, 13, 19, 28, 41, 60, 88, 129, 189, 277, 406, 595, 872

Now let the roots of the caracteristic polynomial, $r^3-r^2-1$ be $r_0, r_1, \overline r_1$ (there is a pair of conjugates). The general solution of the recurrence is

$$c_n=ar_0^n+br_1^n+\overline b\overline r_1^n,$$

where constants are determined by the system of equations

$$a+b+\overline b=1,\\ar_0+br_1+\overline b\overline r_1=1,\\ar_0^2+br_1^2+\overline b\overline r_1^2=1.\\$$

As one can check from the numerical values, the ratio of two successive terms quickly tends to the constant $1.465571231876768$, which is the real root (it has the largest modulus).

By numerical experimentation, it appears that the simple formula yields exact values in a large range:

$$c_n=[0.6114919919508175\cdot1.465571231876768^n]$$

where the brackets denote rounding to the nearest integer.

3

I think this problem is one of the problems related to recurrence relation.

Let $a_n$ be the number of ways.

For example, $a_1 =1$, $a_2 = 1$, $a_3=2$, $a_4=3$ as you mentioned above.


Let's think of the number of ways for $n$. Obviously, it is $a_n$.

If we take 1 chocolate at the first time, then the number of remaining chocolates is $n-1$.

And the number of ways for $n-1$ is $a_{n-1}$.

If we take 3 chocolate at the first time, then the number of remaining chocolates is $n-3$.

And the number of ways for $n-3$ is $a_{n-3}$.

Therefore, for $n \geq 4$,

$$a_n = a_{n-1} + a_{n-3}.$$

The initial value is given as above.

with-forest
  • 1,175
  • 1
    wonderful. I have calculated values till n=10 manually. and this formula seems to agree with our findings. Thanks man :) – Shobi Jan 31 '19 at 11:00