C # obtain all the prime numbers in the screening range,

Source: Internet
Author: User

C # obtain all the prime numbers in the screening range,

   

Popular Science: screening is a simple algorithm for prime number verification. It is said to be Eratosthenes of ancient Greece, around 274-BC ~ Invented in 194, also known as the sieve of Eratosthenes method ).

   

To be honest, I used to verify whether a certain number is a prime number in the case of qualitative quantity. I can easily draw a conclusion by using definition. The Code is as follows:

   
01: public static bool IsPrime (int n) 02: {// determine whether n is a prime number 03: if (n <2) return false; 04: for (int I = n-1; I> 1; I --) 05: {// n divided by each natural number greater than 1, which is smaller than n 06: if (n % I = 0) 07: {// if divisible, it is not a prime number 08: return false; 09:} 10 :} // otherwise, it indicates the prime number 11: return true; 12 :}
  

However, if we use this method to determine all the prime numbers between two numbers x and y, We need to judge them cyclically:

  
1:  for (int i = x; i < y; i++)2:  {3:      if (IsPrime(i))4:      {5:          Console.Write(i);6:      }7:  }
  
Today, I accidentally saw that the screening method may be more suitable for solving such problems-finding all the prime numbers within a certain upper limit:
  
01:  private static List<int> GenPrime(int j)02:  {03:      List<int> ints=new List<int>();04:      BitArray bts=new BitArray(j+1);05:      for (int x = 2; x < bts.Length / 2; x++)06:      {07:          for (int y = x + 1; y < bts.Length; y++)08:          {09:              if (bts[y] == false && y % x == 0)10:              {11:                  bts[y] = true;12:              }13:          }14:      }15:      for (int x = 2; x < bts.Length; x++)16:      {17:          if (bts[x] == false)18:          {19:              ints.Add(x);20:          }21:      }22:      return ints;23:  }
  

However, if the prime number in the range is required, the difference set of the two ranges must be required:

  
1:  List<int> ListResult = GenPrime(x).Except(GenPrime(y)).ToList();
 
 
  
Then I found a linear screening algorithm in another blog. I changed it to C # code:
  
01:  private static List<int> GenPrime1(int x)02:  {03:      int num_prime = 0;04:      List<int> ints = new List<int>();05:      BitArray isNotPrime = new BitArray(x);06:      for (int i = 2; i < x; i++)07:      {08:          if (!isNotPrime[i])09:          {10:              ints.Add(i);11:              num_prime++;12:          }       13:          for (int j = 0; j < num_prime && i * ints[j] < x; j++)14:          {15:              isNotPrime[i * ints[j]] = true;16:              if (!Convert.ToBoolean(i % ints[j]))                  17:                  break;18:          }19:      }20:      return ints;21:  }
  
Sent to the original post: prime number obtained by ordinary sieve + fast linear sieve
  
PS. I wrote a blog for the first time. If there are any deficiencies, please let me know. I must change it!

Related Article

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.