Solves Problem 48 in Python