Slightly optimized Problem 25.