# A simple code for conditional double summation?

I wrote a code as follows

f[n_] := n^2 + 1;
x = 10; y = 2 x; d = 2;
A = Table[f[n], {n, y - x + 1, y}];
A1[d_] := Select[A, Mod[#, d] == 0 &]

Sum[f[n], {f[n], A1[d]}]

Now I want to write the Mathematica code for the following double summation:

$$\Large \sum_{{1\le l\le d}\atop{f(l)\equiv 0,\bmod d}}\sum_{{Y-X< n\le X}\atop{n\equiv l,\bmod d}} 1$$

Please give a simple and elementary code that is not complicated or advanced.

I guess the following would do:

Sum[
Boole[(Mod[f[l], d] == 0) && (Mod[n - l, d] == 0)],
{l, 1, d}, {n, y - x + 1, x - 1}
]

This works because Boole converts True to 1 and False to 0.

Sum[If[Mod[f[l], d]==0 && Mod[n-l, d] == 0, 1, 0], {l, 1, d}, {n,y-x+1,y}]
• Does replacing Boole with If[#, 1,0]& deserve a separate answer? – Kuba Jan 15 '18 at 11:15
• @Kuba, you are right. at the beginning when I compile his first code, it only gave $0$, but when I run it now it is OK! – asad Jan 15 '18 at 11:28
• @asad: It returned 0 because y-x equaled x in your example. So it was correct right from the start... – Henrik Schumacher Jan 15 '18 at 15:16