const
newline=#13; {Newline character}
{************** SumConsec ***************}
function Sum(lo,hi:integer):integer;
{Returns the sum of consecutive integers
from lo to hi, including lo and hi}
var
n:integer;
begin
if lo>hi then {if lo is not smaller, exchange lo and hi}
begin
n:=lo; lo:=hi; hi:=n;
end;
{The sum of a consecutive set of integers is
the number of integers: (hi-lo+1)
times the average integer: (lo+hi) divided by 2 }
result:= (hi-lo+1)*(lo+hi) div 2;
end;
{**************** SolveButtonClick ****************}
procedure TForm1.SolveButtonClick(Sender: TObject);
var
nbrRunners,MyNumber:integer;
maxrunners:integer;
x:integer;
begin
NbrRunners:=strtoint(MinNbrEdt.text)+1; {Start NbrRunners at min nbr}
Maxrunners:=strtoint(MaxNbrEdt.text)-1; {Set stop point at max runners}
{Loop to check all possible nbrrunners}
while (NbrRunners<=maxrunners) do
begin
MyNumber:=1;
{Loop to check all possible marathoners nbrs for this nbrrunners}
while (MyNumber<=NbrRunners) do
begin
screen.cursor:=crHourGlass;
x:=sum(1,MyNumber-1);
if x=sum(MyNumber+1,NbrRunners) then
begin
screen.cursor:=crDefault;
showmessage('One solution is:'
+newline + 'Number of runners: '+inttostr(NbrRunners)
+newline + 'Our runner''s number: '+inttostr(MyNumber)
+newline + 'Sum of numbers 1:'+inttostr(MyNumber-1)
+ ' and '+inttostr(MyNumber+1) + ':'
+ inttostr(NbrRUnners)+' is '+format('%6d',[x])
);
{Once we've found a solution,
force this loop to stop since no bigger values for MyNumber will work}
MyNumber:=NbrRunners;
end;
inc(MyNumber); {add 1 to MyNumber}
end;
inc(NbrRunners); {add 1 to NbrRunners}
end;
screen.cursor:=crdefault;
x:=maxrunners+1;
Showmessage('No more solutions with less than '+format('%6d',[x])+' runners');
end;