Updated the algorithm to use a sieve rather than calling isPrime() for every number.