Solves Problem 6 in Python