Solves Problem 23 in Python