Solves Problem 17 in Python