Problem 40 completely solved