Solves Problem 50 in Python