Solves Problem 46 in Python