Solves Problem 30 in Python