2
$\begingroup$

Could someone please explain mathematical explanation behind this?

You can win three kinds of basketball points, 1 point, 2 points, and 3 points. Given a total score $n$, print out all the combination to compose $n$.

Examples:
For n = 1, the program should print following:
1

For n = 2, the program should print following:
1 1
2

For n = 3, the program should print following:
1 1 1
1 2
2 1
3

For n = 4, the program should print following:
1 1 1 1
1 1 2
1 2 1
1 3
2 1 1
2 2
3 1

Algorithm:

  • At first position we can have three numbers 1 or 2 or 3. First put 1 at first position and recursively call for n-1.
  • Then put 2 at first position and recursively call for n-2.
  • Then put 3 at first position and recursively call for n-3.
  • If n becomes 0 then we have formed a combination that compose n, so print the current combination.

I've solved in using JS as per below. But I don't quite understand the mathematical reasoning behind it.

https://jsfiddle.net/d2Lft7d1/

2 Answers 2

1

It is a Fibonacci like sequence.
Let $\,A_{\small n}\,$ be the total number of $\,(1,\,2,\,3)\,$ combinations that compose $\,n\,$, Then: $$ A_{\small 1}=1,\,A_{\small 2}=2,\,A_{\small 3}=4,\quad\color{red}{A_{\small n}=A_{\small n-1}+A_{\small n-2}+A_{\small n-3}} \\[4mm] \Rightarrow\quad \left\{A_{\small n}\right\}=\left\{1,\,2,\,4,\,7,\,13,\,24,\,44,\,\cdots\right\} $$ And the idea behind that for $\,n\gt3\,$, you will have the ability to add a Most Significant Digit (MSD) equals $\,1,\,2,\text{ or } \,3\,$. This should left you with $\,n-1,\,n-2,\text{ and } \,n-3\,$ respectively. For Example: $$ \begin{align} n &=5 \\[2mm] \text{MSD} &=\color{red}{1} \quad\Rightarrow\text{ The comination of }\,(n-1=4)= \begin{cases} \color{red}{1}\,1\,1\,1\,1 \\ \color{red}{1}\,1\,1\,2 \\ \color{red}{1}\,1\,2\,1 \\ \color{red}{1}\,1\,3 \\ \color{red}{1}\,2\,1\,1 \\ \color{red}{1}\,2\,2 \\ \color{red}{1}\,3\,1 \end{cases} \\[2mm] \text{MSD} &=\color{blue}{2} \quad\Rightarrow\text{ The comination of }\,(n-2=3)= \begin{cases} \color{blue}{2}\,1\,1\,1 \\ \color{blue}{2}\,1\,2 \\ \color{blue}{2}\,2\,1 \\ \color{blue}{2}\,3 \end{cases} \\[2mm] \text{MSD} &=\color{Green}{3} \quad\Rightarrow\text{ The comination of }\,(n-3=2)= \begin{cases} \color{Green}{3}\,1\,1 \\ \color{Green}{3}\,2 \end{cases} \\[2mm] A_{\small5} &= \color{red}{A_{\small4}}+\color{blue}{A_{\small3}}+\color{green}{A_{\small2}} = \color{red}{7}+\color{blue}{4}+\color{green}{2} = 13 \end{align} $$


For other similar combination $\,\left({\small\text{e.g }}\,(1,2)\,,(1,2,4)\,,\cdots\right)\,$, we start by computing the first required terms, then we apply the concept of Fibonacci sequence and Most Significant Digit (MSD).

$\underline{\bf(1,2)}$: $$ \begin{align} (n=1) &\rightarrow \begin{cases} \color{red}{1} \end{cases} \qquad\Rightarrow\, A_{\small 1}=1 \\[2mm] (n=2) &\rightarrow \begin{cases} \color{blue}{1}\,\color{red}{1} \\ \color{blue}{2} \end{cases} \quad\Rightarrow\, A_{\small 2}=2 \\[2mm] A_{\small n} &= A_{\small n-1}+A_{\small n-2} = \left\{1,\,2,\,3,\,5,\,8,\,13,\,21,\,\cdots\right\} \end{align} $$ $\underline{\bf(1,2,4)}$: $$ \begin{align} (n=1) &\rightarrow \begin{cases} \color{red}{1} \end{cases} \qquad\qquad\Rightarrow\, A_{\small 1}=1 \\[2mm] (n=2) &\rightarrow \begin{cases} \color{blue}{1}\,\color{red}{1} \\ \color{blue}{2} \end{cases} \quad\qquad\Rightarrow\, A_{\small 2}=2 \\[2mm] (n=3) &\rightarrow \begin{cases} \color{green}{1}\,\color{blue}{1}\,\color{red}{1} \\ \color{green}{1}\,\color{blue}{2} \\ \color{green}{2}\,\color{red}{1} \end{cases} \qquad\Rightarrow\, A_{\small 3}=3 \\[2mm] (n=4) &\rightarrow \begin{cases} 1\,\color{green}{1}\,\color{blue}{1}\,\color{red}{1} \\ 1\,\color{green}{1}\,\color{blue}{2} \\ 1\,\color{green}{2}\,\color{red}{1} \\ 2\,\color{blue}{1}\,\color{red}{1} \\ 2\,\color{blue}{2} \\ 4 \end{cases} \quad\Rightarrow\, A_{\small 4}=6 \\[2mm] A_{\small n} &= A_{\small n-1}+A_{\small n-2}+A_{\small n-4} = \left\{1,\,2,\,3,\,6,\,10,\,18,\,31,\,\cdots\right\} \end{align} $$

  • 0
    OK... but now how do you get number of combinations to be 7, 4 and 2 ?2017-03-05
  • 0
    @MonteCristo : Example: for $n=4$ all possible combinations are 7 "which also the result of Fibonacci like sequence that stated above". Now, for $n=5$, you can put $1$ as MSD, remaining is $4$ gives the $7$ combinations. and so on.2017-03-05
  • 0
    pardon me... algorithms are not my strongest point. Essentially what I am trying to figure is make it generic so I would be able to use it to find (1,2) combinations adds up to 5. So trying to wrap my head around (n-1 = 4) => 7 combinations. if I do (1,2) combinations I get (n-1= 4 => 5 + n-2=3 => 3) 8.2017-03-05
  • 0
    @MonteCristo : Helping you is a pleasure. If you are trying to know number of combinations, then you need to analyze the **Generating Sequence**. So, How can I help you?2017-03-05
  • 0
    Thanks... :) patience is greatly appreciated. Issue I have is (1,2,3) combination could change e.g. (1,2,4) or (1,2). So bit stuck with how to incorporate what you highlighted above. Also I noticed An = 6 => 24 combinations not 26?2017-03-05
  • 0
    @MonteCristo : 1st, indeed 13+7+4=24, it is a typo and I am fixing it now. Thanks.2017-03-05
  • 0
    @MonteCristo 2nd, let me try to update my answer, hopefully it well be good enough to help.2017-03-05
0

Consider the set of sequences of points that add up to $n$ and let's call it $S(n)$. There are three different cases to consider regarding the type of sequence we might have.

  1. The first point value is 1. This can only happen if the sum of the remaining points is $n-1$ so the rest of the subsequence must be an element of $S(n-1)$.

  2. The first point value is 2. This can only happen if the sum of the remaining points is $n-1$ so the rest of the subsequence must be an element of $S(n-2)$.

  3. The first point value is 3. This can only happen if the sum of the remaining points is $n-3$ so the rest of the subsequence must be an element of $S(n-3)$.

These are the only possible cases, each of which is mutually exclusive so the algorithm will print out every possible sequence exactly once.

EDIT: As an example, look at your $n=4$ case, in other words, sequences in $S(4)$. Notice that every sequence is one of the three forms:

  1. The number $1$ followed by some sequence in the $n=3$ case since $S(n-1) = S(3).$
  2. The number $2$ followed by some sequence in the $n=2$ case since $S(n-1) = S(2).$
  3. The number $3$ followed by some sequence in the $n=1$ case since $S(n-1) = S(1).$