The search for prime numbers has long been a goal that some mathematicians have pursued. On the definition and nature of prime numbers, I am not here, I believe we all know this. The way to find the prime number is simpler, depending on the nature of the prime number (primes should not be divisible by 1 and other numbers of its own) we can start with the smallest prime number 2, until it is 1 smaller than it, and divide it by these numbers, and if it is divisible it must not be prime, This is the method of judging a single prime number (this algorithm is the simplest and most complex time). This method can be used to find all primes that are smaller than a given integer value, but we will find that the method of using this single judgment is more time-consuming. For example, to find a prime number of not more than 10, we have to start from 2 to judge, a total of 9 to judge, in fact, according to the way we tell later, only a cycle of 2 times on it. Therefore, both of the following methods will be based on the deletion method.
Let's take a look at the idea of deleting the law:
1. Adds all positive integers that are less than the given integer value n to an array;
2. Deletes the number that can be divisible by some integers;
3. The element left in the array is the last prime sequence to be obtained.
For the second step, we'll give you two ways to implement it. Let's take a look at the algorithm first:
Algorithm one:
class prime
{
public static int[] PrimeList;
public static void FindPrime(int n)
{
int[] IntList;
IntList=new int[n];
for (int p=2;p<=n;p++) IntList[p-1]=p;
for (int p=2;p<Math.Sqrt(n);p++)
{
int j=p+1;
while (j<=n)
{
if ((IntList[j-1]!=0 ) && ((IntList[j-1]% p)==0) ) IntList[j-1]=0;
j=j+1;
}
}
int i=0;
for (int p=2;p<=n;p++)
{
if (IntList[p-1]!=0) i=i+1;
}
PrimeList=new int[i];
i=0;
for (int p=2;p<=n;p++)
{
if (IntList[p-1]!=0)
{
PrimeList[i]=IntList[p-1];
i=i+1;
}
}
}
}