All possible ways scores $(1,2,3)$ add up to $n$ 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/
 A: 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} $$
A: 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.


*

*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)$.

*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)$.

*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:


*

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

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

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

