0
$\begingroup$

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.

$\endgroup$

2 Answers 2

1
$\begingroup$

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.

$\endgroup$
0
$\begingroup$
Sum[If[Mod[f[l], d]==0 && Mod[n-l, d] == 0, 1, 0], {l, 1, d}, {n,y-x+1,y}]
$\endgroup$
3
  • $\begingroup$ Does replacing Boole with If[#, 1,0]& deserve a separate answer? $\endgroup$
    – Kuba
    Commented Jan 15, 2018 at 11:15
  • $\begingroup$ @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! $\endgroup$
    – asad
    Commented Jan 15, 2018 at 11:28
  • $\begingroup$ @asad: It returned 0 because y-x equaled x in your example. So it was correct right from the start... $\endgroup$ Commented Jan 15, 2018 at 15:16

Your Answer

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

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