Solution for Problem #8
Solution for Problem #8 (optimized)