Faster version for Problem 10