Solves Problem 5 in Python