Thanks, Felipe!
This is very similar to the Problem 14 on Project Euler [1]. Should
you really want a nice solution in Haskell, and noting that this is a
big spoiler, there are some on the wiki [2]. But I highly recommend
you to try coding your own solutions before looking at the site =).
Cheers!
[1] http://projecteuler.net/index.php?section=problems&id=14
[2] http://www.haskell.org/haskellwiki/Euler_problems/11_to_20#Problem_14
--
Felipe.