Solved problem 8 in python and c++.