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.




[indent=4]
/*
   Sum of series, in Genie
   valac sumOfSeries.gs
   ./sumOfSeries
*/
 
delegate sumFunc(n:int):double
 
def sum_series(start:int, end:int, f:sumFunc):double
    sum:double = 0.0
    for var i = start to end do sum += f(i)
    return sum
 
 
def oneOverSquare(n:int):double
    return (1 / (double)(n * n))
 
init
    Intl.setlocale()
    print "ζ(2) approximation: %16.15f", sum_series(1, 1000, oneOverSquare)
    print "π² / 6            : %16.15f", Math.PI * Math.PI / 6.0