import java.util.Arrays;
public class PrimeNumberGenerator
{
public static int[] getPrimes(int min, int max)
{
int[] result = getOddPrimes(min, max);
if (min <= 2 && max >= 2) result = add2(result);
return result;
}
private static int[] getOddPrimes(int min, int max)
{
if (max < 3 || max < min) return new int[0];
int size = max / 2;
int[] oddPrimes = new int[size];
int index = 0;
int minIndex = -1, maxIndex = -1;
for (int n = 3; n <= max; n += 2)
{
int nDiv2 = n / 2;
boolean isPrime = true;
for (int i = 0; i < index; i++)
{
int p = oddPrimes[i];
if (p > nDiv2) break;
if (n % p == 0)
{
isPrime = false;
break;
}
}
if (isPrime)
{
if (n >= min && minIndex == -1) minIndex = index;
maxIndex = index;
oddPrimes[index++] = n;
}
}
if (minIndex == -1) return new int[0];
size = maxIndex - minIndex + 1;
int[] result = new int[size];
System.arraycopy(oddPrimes, minIndex, result, 0, size);
return result;
}
private static int[] add2(int[] array)
{
int[] result = new int[array.length + 1];
result[0] = 2;
System.arraycopy(array, 0, result, 1, array.length);
return result;
}
}