I want to sum over a function of n-tuple like so:

$\sum\limits_{a\leq k_1+k_2+...+k_n\leq b}f(\{k_i\}_{i=1}^{n})$

where each $k_i$ is non-negative, i.e. $k_i\geq0 \;\forall\;i $

An Example:

Say, we want to evaluate $\sum\limits_{2\leq k_1+k_2+k_3\leq5}k_1^{k_1!}k_2^{k_2!}k_3^{k_3!}$

My attempt:

tuplelist[x_,n_]:=Sequence @@ Permutations[Join[#, ConstantArray[0, n - Length@#]]] & /@ IntegerPartitions[x, n];

Please suggest if there are ways to modify the existing Sum[] function or other efficient ways to do this. Looking forward to a variety of techniques!


3 Answers 3


Anjan Kumar's answer is good, but if you want to be able to give the function an expression (like with an ordinary Sum), here's one possibility (edited to make use of J. M.'s suggestion of FrobeniusSolve):

Attributes[SumOverIntegerPartitions] = {HoldFirst};
   f_, {ks_List, min_Integer?Positive, max_Integer?Positive}] := 
  With[{perms =
      FrobeniusSolve[ConstantArray[1, Length@ks], #] & /@ 
       Range[min, max]]}, 
   Total[f /. (Thread[ks -> #] & /@ perms)]];

Using the same test case:

SumOverIntegerPartitions[k1^k1!*k2^k2!*k3^k3!, {{k1, k2, k3}, 2, 5}]
(* 2248 *)

You can also use FrobeniusSolve[] for this sum, since it automatically generates 0 in its tuples, unlike IntegerPartitions[]:

Sum[Product[k^k!, {k, m}],
    {m, Flatten[Table[FrobeniusSolve[ConstantArray[1, 3], k], {k, 2, 5}], 1]}]

You can modify your code to something like this:

f[k_] := Times @@ (#^#! & /@ k);
tuplelist[x_, n_] := Permutations /@ IntegerPartitions[x, {n}, Range[0, x]];
Outer[f, tuplelist[#, 3] & /@ Range[2, 5], 3] // Total[#, 3] &



Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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