Solves Problem 29 in Python