Optimizes problem12 after learning about 'scanl' and 'product' functions