# Skip certain values in a sum [duplicate]

How can I evaluate such a sum:

$$\sum_{j=0, j\neq10}^{J} f(j)$$

Since I am trying to do some symbolic calculations, solutions such as the one below are undesirable:

$$-f(10) + \sum_{j=0}^{J} f(j)$$

Thanks.

You can start by creating a list of all the values you want to iterate through:

indices = DeleteCases[Range[0,J],10];


And then do either

Sum[f[i], {i, indices}]


or

Total[f /@ indices]


You can use an If statement:

Sum[If[i == 10, 0, f[i]], {i, J}]

• It's probably equivalent but I find this syntax more elegant: Sum[f[j] Boole[j != 10], {j, 0, J}]
– user484
Commented Nov 4, 2014 at 13:04

There are two ways basically. First one:

Sum[f(j)*Boole[j != 10], {j, 0, 10}]


Another way is:

Sum[If[j != 10, Sin[1] Cos[j], 0], {j, 0, 10}]


See this link too: How do you put conditions on indices in a sum?

The solutions given above are correct, and probably the best way to go.

But if you can be absolutely certain that f(10) is not an expensive call, it might even be better to do something like what you are saying, since it would avoid having to making a comparison for each index i.

So you could do the following:

g[x_] := f[x];
g[10] := 0;
Total[g /@ Range[100000]]