static int[] a = new int[100];
static int index = 3;
static
{
a[0]=2;
a[1]=3;
a[2]=5;
a[3]=7;
}
public int nextPrime()
{
//creat an arry.
//divide the number with all the prime numbers you have so far.
//if none of them results in a reminder of zero,
//then the number must be prime.
//you add this new prime number to the collection of primes you have so far.
//then return the prime number
int n = a[index] + 1;
while(true)
{
bool b = true;
for(int i=0; i<index ; i++)
{
if( n % a[i] == 0 ) {
n++;
b = false;
break;
}
}
if( b ) { // n is prime, save n into the collection of primes, and increase index by one.
a[index++] = n;
return n;
}
// else try the next n;
}
}