slightly faster solution to problem 32