C # algorithm design and analysis-find primes

Source: Internet
Author: User

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;
          }         
       }
     }
   }

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.