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.




/*REXX program sums the first    N    terms of     1/(k**2),          k=1 ──►  N.       */
parse arg N D .                                  /*obtain optional arguments from the CL*/
if N=='' | N==","  then N=1000                   /*Not specified?  Then use the default.*/
if D=='' | D==","  then D=  60                   /* "      "         "   "   "     "    */
numeric digits D                                 /*use D digits (9 is the REXX default).*/
$=0                                              /*initialize the sum to zero.          */
          do k=1  for N                          /* [↓]  compute for   N   terms.       */
          $=$  +  1/k**2                         /*add a squared reciprocal to the sum. */
          end   /*k*/
 
say 'The sum of'     N     "terms is:"    $      /*stick a fork in it,  we're all done. */