Solves Problem 38 in Python