Compute the   nth   term of a series,   i.e. the sum of the   n   first terms of the corresponding sequence.

Informally this value, or its limit when   n   tends to infinity, is also called the sum of the series, thus the title of this task.

For this task, use:

 




S

n


=

∑

k
=
1


n




1

k

2






{\displaystyle S_{n}=\sum _{k=1}^{n}{\frac {1}{k^{2}}}}




 and compute   




S

1000




{\displaystyle S_{1000}}




This approximates the   zeta function   for   S=2,   whose exact value

 



ζ
(
2
)
=



π

2


6




{\displaystyle \zeta (2)={\pi ^{2} \over 6}}


is the solution of the Basel problem.




 
unit Form_SumOfASeries_Unit;
 
interface
 
uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls;
 
type
  TFormSumOfASeries = class(TForm)
    M_Log: TMemo;
    B_Calc: TButton;
    procedure B_CalcClick(Sender: TObject);
  private
    { Private-Deklarationen }
  public
    { Public-Deklarationen }
  end;
 
var
  FormSumOfASeries: TFormSumOfASeries;
 
implementation
 
{$R *.dfm}
 
function Sum_Of_A_Series(_from,_to:int64):extended;
begin
  result:=0;
  while _from<=_to do
  begin
    result:=result+1.0/(_from*_from);
    inc(_from);
  end;
end;
 
procedure TFormSumOfASeries.B_CalcClick(Sender: TObject);
begin
  try
    M_Log.Lines.Add(FloatToStr(Sum_Of_A_Series(1, 1000)));
  except
    M_Log.Lines.Add('Error');
  end;
end;
 
end.
 
 