Solution to 5. C much faster than Haskell here.