EDIT, due to your recent clarification of the post.
You won't get away with a hand made solution, unless you have a whole team of PhDs and several years to spend. The best advice I can give you is to buy a Mathematica (or other) license and interface it with your program.
If you are a Lisp programmer, using Maxima is another potential (free this one) solution.
If you want background on the state of art in summation algorithms, this paper is a good start.
X1+X2+...+Xk=n, where Xi is integer and >= 0.
What's the Expectation of X1^2+...Xk^2?
This kind of problems occupy a lot of people to figure out how to do it on paper.
Let us take k = 2. Then X_1 + X_2 = n gives X_2 = n - X_1.
So the expectation to be computed is E = X_1^2 + (n - X_1)^2 = 2 X_1^2 -2n X_1 + n^2
.
This reads
E = sum(p_k * (2 * k^2 - 2 * n * k + n^2), k = 0..infinity)
where p_k = Prob(X_1 = k)
. This kind of sums, depending on p_k
, is generally very difficult to compute. I'd say that the problem is even more difficult than computing integrals in closed form (for which no software fully implement the available -- but undecidable -- Risch algorithm).
To convince yourself, take eg. p_k = 1 / (log(k) * k^4)
.
Finding a formula (or a formula generator) for it is at the very least a very difficult research problem.