faster problem3 solution