Slightly optimized PHP solution to Problem 6.