Solve problem 51 using Python.