Solve problem 8 using Python.