Slightly optimized Problem 5.