Skip to content

Instantly share code, notes, and snippets.

@ljrk0
Created November 24, 2015 09:45
Show Gist options
  • Save ljrk0/d65ddccdc5c93e19ab02 to your computer and use it in GitHub Desktop.
Save ljrk0/d65ddccdc5c93e19ab02 to your computer and use it in GitHub Desktop.
-- sum_{k=1}^{n} k^2 :
qSum n = foldr (+) 0 (map (^2) [1..n])
qSumC :: Fractional a => a -> a
qSumC n = n * (2*(n^2) + 3*n + 1) / 6
-- bug in haskell?:
--
-- try in interactive mode:
-- let qs n = n* (2*(n^2) + 3*n + 1) / 6
-- it will give 14.0 for n=3
--
-- qSumC 3 will be 1015.0 ...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment