Create an optimized program to generate prime numbers up to a given limit. Explain the mathematical basis of your optimization.