MySum[p_List, iMin_] := Module[{printOuput, i, iMax},
iMax = iMin + Length[p];
printOuput = "";
For[i = iMin, i < iMax, i++,
printOuput =
printOuput <> " + " <> ToString[i, TraditionalForm] <>
" \[CenterDot] " <>
ToString[Part[p, i - iMin + 1], TraditionalForm]];
Print[printOuput <> " = "];
Sum[Part[p, i - iMin + 1]*i, {i, iMin, iMax - 1}]];
p = 1/7 {3, 2, 0, 2};
MySum[p, 0]
This is my code.
- I would like to have it in one separate line.
- I would also like to remove the first "+",
So the output will look exactly like this:
$$0 \cdot \frac{3}{7} + 1 \cdot \frac{2}{7} + 2 \cdot 0 + 3 \cdot \frac{2}{7}= \frac{8}{7}$$
Print[...]
line and change the next one toStringDrop[printOuput, 2] <> " = " <> ToString[Sum[...], TraditionalForm]
$\endgroup$