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
Mathematica Stack Exchange is a question and answer site for users of Wolfram Mathematica. It only takes a minute to sign up.
Sign up to join this communityh[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
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