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.




series_sum = proc (from, to: int, 
                   fn: proctype (real) returns (real))
             returns (real)
    sum: real := 0.0
    for i: int in int$from_to(from, to) do
        sum := sum + fn(real$i2r(i))
    end
    return(sum)
end series_sum
 
one_over_k_squared = proc (k: real) returns (real)
    return(1.0 / (k * k))
end one_over_k_squared
 
start_up = proc ()
    po: stream := stream$primary_output()
    result: real := series_sum(1, 1000, one_over_k_squared)
    stream$putl(po, f_form(result, 1, 6))
end start_up