4
$\begingroup$

Is it possible in Mathematica (or WolframAlpha["...]") to get a step-by-step evaluation of the following?

    f[n_, x_] := 
 Sum[(-1)^m (n!/(m! (n - 2 m)!)) (2 x)^(n - 2 m), {m, 0, Floor[n/2]}]
f[3, x]
$\endgroup$

2 Answers 2

3
$\begingroup$
Clear["Global`*"];
f[n_, x_] := 
 Sum[(-1)^m (n!/(m! (n - 2 m)!)) (2 x)^(n - 2 m), {m, 0, Floor[n/2]}]
trace = Trace@f[6, x];
steps = trace[[3 ;; -2 , -1]] // ReleaseHold;
acc = steps // Accumulate;

{Range@Length@steps, steps, acc} //
   Transpose //
  Prepend[#, { "Step #", "Current Term", "Accumulated Sum"}] & //
 Grid[#, Dividers -> All, Spacings -> {1, 1}] &

enter image description here

$\endgroup$
6
$\begingroup$
f[n_, x_] := Module[{sum = 0, currentTerm},
  First@Last@Reap@Do[
      currentTerm = (-1)^m (n!/(m! (n - 2 m)!)) (2 x)^(n - 2 m);
      Sow[{m, currentTerm, sum += currentTerm}],
      {m, 0, Floor[n/2]}
      ]
  ]

Now do

data = f[6, x];
Grid[PrependTo[data, {"m", "current term", "current sum"}], 
 Frame -> All]

Mathematica graphics

$\endgroup$

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.