Solves Problem 45 in Python