Solves Problem 41 in Python