Solved 31 and 76. TODO: translate to Haskell.