SumOfSeries Routines
Algebraically calculates the sum of a series.

Unit
QESBPCSMath

Overloaded Variants
Function SumOfSeries(EndValue: LongWord): Int64;
Function SumOfSeries(StartValue, EndValue: LongWord): Int64;

Declaration
Function SumOfSeries(EndValue: LongWord): Int64;

Description
If only a single parameter is used, then it assumes that 1 is the StartValue. Returns the sum of all the integers between StartValue & EndValue inclusive. Optimised BASM supplied by Marcel Martin.

Parameters
StartValue Value to Start Summation from. Optional - 1 is assumed if omitted.
EndValue Last Value of Series to Sum.

Category
Arithmetic Routines for Integers

Implementation

function SumOfSeries (EndValue: LongWord): Int64;
asm
	mov  ecx, eax
	mul  eax       //(edx:eax) := EndValue˛
	add  eax, ecx  //(edx:eax) := EndValue˛ + EndValue
	adc  edx, 0
	shr  edx, 1    //Result := (edx:eax) shr 1
	rcr  eax, 1    //rcr ~ Rotate Carry Right
End;

Declaration
Function SumOfSeries(StartValue, EndValue: LongWord): Int64;

Implementation

function SumOfSeries (StartValue, EndValue: LongWord): Int64;
begin
     if EndValue = StartValue then
     begin
          Result := StartValue;
          Exit;
     end
     else if EndValue < StartValue then
     begin
          Result := 0;
          Exit;
     end;
     Result := SumOfSeries (EndValue) - SumOfSeries (StartValue) + StartValue;
End;


HTML generated by Time2HELP
http://www.time2help.com