Solution for Problem #21 (Brute-force)