Haskell #5: Mathematical solution (inspired)

Inspired by:
http://stackoverflow.com/a/147539