Say I wanted to calculate something like

With Haskell:

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

21049

This can be generalised to something like the following:

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

Example:

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

385

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

21049

On paper, I’d have to

- split this into two sums:
- find an expression for using standard sums of the first n squares and naturals, giving
- substitute n = 40 and n = 9 into this, and subtract the latter from the former

Advertisements

[…] Snippet – Summing Series #2 Just a little hack using show to make the earlier summing series example look a bit […]