I would like to create a function
f[r_, x_]
which returns $$\begin{align*}\sum_{i_{r-1} = 1}^{f^{r-1}} \sum_{i_{r-2}=1}^{f^{r-2}} \cdots \sum_{i_2 = 1}^{f^2} \sum_{i_1 = 1}^{f} \frac{ \binom{n}{i_1} i_1! \begin{Bmatrix} f \\ i_1\end{Bmatrix} }{n^f} \cdot \frac{ \binom{n}{i_2} i_2! \begin{Bmatrix} f \cdot i_1 \\ i_2\end{Bmatrix} }{n^{f \cdot i_1}} \cdots \frac{ \binom{n}{i_{r-1}} i_{r-1}! \begin{Bmatrix} f \cdot i_{r-2} \\ i_{r-1}\end{Bmatrix} }{n^{f \cdot i_{r-2}}} \cdot \frac{ \binom{n}{x} x! \begin{Bmatrix} f \cdot i_{r-1} \\ x\end{Bmatrix} }{n^{f \cdot i_{r-1}}}\end{align*}$$ where $\begin{Bmatrix} a \\ b\end{Bmatrix}$ represents the Stirling numbers of the second kind.

My problem is to automatize the creation of new sums as a function of $r$.

How should I proceed to create this function?

Thanks for any help

Edit: I tried to inspire myself from this question to solve it, and I have the following code for the moment:

n = 1000;
f = 4;
g[x_, y_] := (Binomial[n, y]*y!*StirlingS2[f*x, y])/n^(f*x);
computeP[r_, x_] := Module[,
  sumRanges = Table[f^i, {i, 1, r}];
  iterator = Table[{k[m], 1, sumRanges[[m]]}, {m, r}];
  ind = First /@ iterator;
  N[Sum[g[Sequence@ind - 1, Sequence @ ind], 
    Evaluate[Sequence @@ iterator]]]

The problem is that I am not familiar with the syntax /@, @, @@ so I do not really understand what I am coding.

  • $\begingroup$ @Feyre I updated the post to show you what I have done so far. $\endgroup$ – Laurent Hayez Dec 4 '16 at 14:36
  • $\begingroup$ Highlight the symbol you do not unserstand (like @ or /@ or #) and press F1, this will show you what function these symbols represent. $\endgroup$ – bill s Dec 4 '16 at 23:57

Instead of trying to understand all the levels of Map and Apply, let's try a little string hacking.

Suppose r=6 giving five levels of Sum. Introduce i0 and i6 to make pattern more uniform, you must manually keep that i6 in sync with r=6.

"," <>
{subscript,1,r-1}]],-1] <>

When you evaluate that you get the string


If you wrap ToExpression[] around that string and define the whole thing as a function then I think you get your desired result.

And if you change that to r=12 and i12=x then it gives you eleven nested Sums.

You should carefully check all this to make certain that I haven't let any errors slip into the code.


An alternate method that accomplishes the same thing while being shorter, easier to read and thus perhaps less error prone. Just have to watch for string replace collisions.

r = 6; i0 = 1; i6 = x;
"Sum[" <>
 {"z"->ToString[s], "y"->ToString[s-1]}], {s,1,r}]],-1]<>
  "{iz,1,f^z},", "z"->ToString[s]], {s,1,r-1}]],-1]<>
| improve this answer | |
  • $\begingroup$ Awesome! This does the work just as I need! String hacking was a fine idea. Thank you very much. $\endgroup$ – Laurent Hayez Dec 4 '16 at 21:06
  • $\begingroup$ @Laurent Hayez I realized there was a shorter, less error prone method tand I appended that to the first solution. $\endgroup$ – Bill Dec 4 '16 at 23:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.