Suppose I want to find all the possible solutions for the equation below.
$$x_1 + x_2 + \dots + x_k + \dots + x_K = N$$
where
$$x_k \in \text{integer}, \text{i.e.}, x_1,x_2, \dots, x_k \in \left\{0,1,2, \dots, N\right\} $$
Right now I have written a simple script in MATLAB which is basically an exhaustive search algorithm which tests all the possible combinations for $x_k$ and returns the set of $x_k$s result in a summation of $N$. Although this works perfectly fine with small values of $N$ and $K$, as the those parameters become larger, it would take forever to see the result and it requires a gigantic amount of RAM which is beyond the capability of PC.
Hence, I am wondering has anyone seen an algorithm or something in Matlab or anywhere which would be less complex and faster and easier to implement? Any suggestion or hint would be much appreciated.
Edit
Sorry English is not my first language, I am looking for the actual solutions. I am not asking for the total number of possible solutions.
Applying this, we get $\boxed{\binom{N-k+K-1}{K-1}}$ – TheBestMagician Oct 19 '20 at 02:24