Solves Problem 34 in Python