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.




----------------------- SUM OF SERIES ----------------------
 
-- seriesSum :: Num a => (a -> a) -> [a] -> a
on seriesSum(f, xs)
    script go
        property mf : |λ| of mReturn(f)
        on |λ|(a, x)
            a + mf(x)
        end |λ|
    end script
 
    foldl(go, 0, xs)
end seriesSum
 
 
---------------------------- TEST --------------------------
 
-- inverseSquare :: Num -> Num
on inverseSquare(x)
    1 / (x ^ 2)
end inverseSquare
 
on run
    seriesSum(inverseSquare, enumFromTo(1, 1000))
 
    --> 1.643934566682
end run
 
 
--------------------- GENERIC FUNCTIONS --------------------
 
-- enumFromTo :: Int -> Int -> [Int]
on enumFromTo(m, n)
    if m ≤ n then
        set lst to {}
        repeat with i from m to n
            set end of lst to i
        end repeat
        lst
    else
        {}
    end if
end enumFromTo
 
 
-- foldl :: (a -> b -> a) -> a -> [b] -> a
on foldl(f, startValue, xs)
    tell mReturn(f)
        set v to startValue
        set lng to length of xs
        repeat with i from 1 to lng
            set v to |λ|(v, item i of xs, i, xs)
        end repeat
        return v
    end tell
end foldl
 
 
-- Lift 2nd class handler function into 1st class script wrapper 
-- mReturn :: Handler -> Script
on mReturn(f)
    if class of f is script then
        f
    else
        script
            property |λ| : f
        end script
    end if
end mReturn