Sieve of Eratosthenes algorithm optimization