Solves Problem 37 in Python