Solves Problem 32 in Python