Optimize prime number generation