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.




term() {
   b=$1;res=$2
   echo "scale=5;1/($res*$res)+$b" | bc
}
 
sum() {
  (read B; res=$1;
  test -n "$B" && (term $B $res) || (term 0 $res))
}
 
fold() {
  func=$1
  (while read a ; do
      fold $func | $func $a
  done)
}
 
(echo 3; echo 1; echo 4) | fold sum