Haskell Snippet – Summing series

Say I wanted to calculate something like \displaystyle\sum_{i=10}^{40} r^2 - r -1

With Haskell:

> sum[r^2-r-1|r<-[10..40]]

This can be generalised to something like the following:

sumseries what from to = sum[what n|n<-[from..to]]


> sumseries (\r->r^2) 1 10
> sumseries (\r->r^2-r-1) 10 40

On paper, I’d have to

  • split this into two sums: \displaystyle\sum_{i=1}^{40} (r^2 - r -1) - \sum_{i=1}^{9} (r^2 - r -1)
  • find an expression for \displaystyle\sum_{i=1}^{n} (r^2 - r -1) using standard sums of the first n squares and naturals, giving \displaystyle\sum_{i=1}^{n} (r^2 - r -1) = \frac{1}{3}(n-2)n(n+2)
  • substitute n = 40 and n = 9 into this, and subtract the latter from the former
Haskell Snippet – Summing series

One thought on “Haskell Snippet – Summing series

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s