1
$\begingroup$
h[n_] := For[i = 1, i <= n, i++, 
   For[j = 1, j <= n, j++, 
    g = g + i*j]; f = f + g;];
h[10]

I want to define a function to calculate the Sum from i to n from the Sum of j to n of i*j

$\endgroup$
5
$\begingroup$

Maybe you are asking for this?

h[n_] := Block[{i, j, g, f},
  f = 0;
  For[i = 1, i <= n, i++,
   g = 0;
   For[j = 1, j <= n, j++, g = g + i*j];
   f = f + g;
   ];
  f
  ]

However, easier and more efficient ways would be

h[n_] := Sum[i j, {i, 1, n}, {j, 1, n}]

or

h[n_] := Total[Range[1, n]]^2

or

h[n_] := (n (n + 1)/2)^2
$\endgroup$

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.