Solves Problem 22 in Python