PROGRAM SummingTerms(Output);
    {--------------------------------------------------
        Program 4.8 - Compute in four ways the series:
            1 - 1/2 + 1/3* - ... + 1/9999 - 1/10000
    ---------------------------------------------------
            1) left to right in succssion,
            2) left to right, all positive and negative
                terms then subtract,
            3) right to left in succession, and
            4) right to left, all positive and negative
                terms then subtract.

    ---------------------------------------------------
        Question: why the four "identical" sums differ?
    ---------------------------------------------------}

    VAR
        SeriesLR,           { series sum left to right in succession }
        SumLRPos,           { sum of positive terms, left to right   }
        SumLRNeg,           { sum of negative terms, left to right   }
        SeriesRL,           { series sum right to left in succession }
        SumRLPos,           { sum of positive terms, right to left   }
        SumRLNeg,           { sum of negative terms, right to left   }
        PosTermLR,          { next positive term, left to right      }
        NegTermLR,          { next negative term, left to right      }
        PosTermRL,          { next positive term, right to left      }
        NegTermRL : Real;   { next negative term right to left       }

        PairsOfTerms: Integer; { count of pairs of terms }

BEGIN
    SeriesLR := 0;
    SeriesRL := 0;
    SumLRPos := 0;
    SumRLPos := 0;
    SumLRNeg := 0;
    SumRLNeg := 0;

    FOR PairsOfterms := 1 TO 5000 DO
    BEGIN
        PosTermLR := 1 / (2 * PairsOfTerms - 1);
        NegTermLR := 1 / (2 * PairsOfTerms);
        PosTermRL := 1 / (10001 - 2 * PairsOfTerms);
        NegTermRL := 1 / (10002 - 2 * PairsOfTerms);

        SeriesLR  := SeriesLR + PosTermLR - NegTermLR;
        SumLRPos  := SumLRPos + PosTermLR;
        SumLRNeg  := SumLRNeg + NegTermLR;
        SeriesRL  := SeriesRL + PosTermRL - NegTermRL;
        SumRLPos  := SumRLPos + PosTermRL;
        SumRLNeg  := SumRLNeg + NegTermRL;
    END;

    WriteLn(Output, SeriesLR);
    WriteLn(Output, SumLRPos - SumLRNeg);
    WriteLn(Output, SeriesRL);
    WriteLn(Output, SumRLPos - SumRLNeg);
END.
