Solution for Problem #47 (Brute-force)