Solves Problem 13 in Python