Solves Problem 21 in Python