Solves Problem 9 in Python