Rearrange the Alternating Harmonic Series

The alternating harmonic series is a conditioanally convergent series.  One of the bizarre facts about conditionally convergent series is that you can rearrange or re-order the terms to get a series with any sum you want!  

In this applet, you can enter a sum that you want, and the applet will rearrange the terms of the harmonic series in an order that will give you that sum.  In its regular order, the sum of the alternating harmonic series is ln 2.  (Can you see why this is reasonable?  Take a look at Problem Set 13, #3.)

In the diagram, the sum you want is shown in green; the red dot shows you the current partial sum (whose value is also printed).  The terms of the series so far are printed below the diagram.  The applet will show you up to the first 200 terms of the rearranged series.  (For best results, enter a sum between -1 and 1.)

DynamicModule[{sum,animation,goButton,partialSums,expressions},
sum=Log[2];
animation=Show[Graphics[{}],ImageSize->{600,60}];

goButton=Button["Rearrange!",
If[!NumericQ[sum],

(* error, stop *)
CreateDialog[Join[{"Error: Your desired sum is not a number"},{DefaultButton[]}],Modal->True],

animation=Show[Graphics[{}],ImageSize->{600,60}];

(* fine, go *)
Module[{pIndex,nIndex,partialSum,terms,max,partialSumExpression},
terms={};
partialSums={};
partialSum=0;
partialSumExpression={};
expressions={};
max=200;
pIndex=1;
nIndex=1;
Do[
If[partialSum≤sum,
AppendTo[terms,1/(2*pIndex-1)];
partialSum+=1/(2*pIndex-1);
AppendTo[partialSumExpression,
Style["+"<>ToString[1/(2*pIndex-1),TraditionalForm],FontColor->Red,FontSize->14]
];
pIndex++,
AppendTo[terms,-1/(2*nIndex)];
partialSum-=1/(2*nIndex);
AppendTo[partialSumExpression,
Style["-"<>ToString[1/(2*nIndex),TraditionalForm],FontColor->Blue,FontSize->14]
];
nIndex++];
AppendTo[expressions,Partition[partialSumExpression,15,15,{1,1},{}]];
AppendTo[partialSums,partialSum],
{i,1,max}];

animation=Animate[Column[{Style["Current partial sum: "<>ToString[N[partialSums[[i]]]],FontFamily->"Times",FontSize->14],Show[Graphics[{PointSize[0.012],Thickness[0.005],Hue[1/3,1,2/3],Line[{{sum,-0.2},{sum,0.2}}],Text[sum,{sum,0.2},{0,-1.5}],Red,Point[{partialSums[[i]],0}]}],Axes->{True,False},PlotRange->{{sum-1/2,sum+1},{-1,2}},ImageSize->{600,60},AspectRatio->1/10,AxesStyle->Arrowheads[{-0.02,0.02}]],Grid[expressions[[i]]]}],{{i,1,"Number of terms in partial sum"},1,max,1},DefaultDuration->max/2,AnimatorElements->{"ProgressSlider","PlayPauseButton"},DisplayAllSteps->True,AnimationRepetitions->1]
]
]
];

Panel[Column[{
Row[{"Enter the sum you want:",InputField[Dynamic[sum],FieldSize->8],goButton},Spacer[12]],
Dynamic[animation]
}]
]
]