C # determine whether a given large number is a prime number. The target gets the correct calculation result at a fast speed.

Source: Internet
Author: User

The title is a test question. When we see this question, the first reaction is that it is a question of procedural complexity, and the second is an algorithm problem.

Let's take a look at the rules of prime numbers:

Link: http://en.wikipedia.org/wiki/Prime_number

C # verification code:

1         public bool primeNumber(int n){2             int sqr = Convert.ToInt32(Math.Sqrt(n));3             for (int i = sqr; i > 2; i--){4                 if (n % i == 0){5                     b = false;6                 }7             }8             return b;9         }

Obviously, the Program Complexity of the above Code is N.

Let's optimize the code and then look at the following code:

 1         public bool primeNumber(int n) 2         { 3             bool b = true; 4             if (n == 2) 5                 b = true; 6             else 7             { 8                 int sqr = Convert.ToInt32(Math.Sqrt(n)); 9                 for (int i = sqr; i > 2; i--)10                 {11                     if (n % i == 0)12                     {13                         b = false;14                     }15                 }16             }17             return b;18         }

By adding a preliminary judgment, the program complexity is reduced to N/2.

The above two sections of Code determine whether a large number is correct at 100%.

1. Satisfying the large number judgment;

2. Obtain the correct results as quickly as possible;

Obviously, it is not satisfied. I checked the fastest algorithm online to get accurate results. One accepted solution is the Miller-Rabin algorithm.

Link: http://en.wikipedia.org/wiki/Miller%E2%80%93Rabin_primality_test

The basic principle of Miller-Rabin is to increase the speed (I .e., probability hit) by random number algorithm, but sacrifices the accuracy.

Miller-Rabin's judgment on the prime numbers of input large numbers is not necessarily completely accurate, but it is a basic solution for this question.

Miller-Rabin C # code:

 1 public bool IsProbablePrime(BigInteger source) { 2             int certainty = 2; 3             if (source == 2 || source == 3) 4                 return true; 5             if (source < 2 || source % 2 == 0) 6                 return false; 7  8             BigInteger d = source - 1; 9             int s = 0;10 11             while (d % 2 == 0) {12                 d /= 2;13                 s += 1;14             }15 16             RandomNumberGenerator rng = RandomNumberGenerator.Create();17             byte[] bytes = new byte[source.ToByteArray().LongLength];18             BigInteger a;19 20             for (int i = 0; i < certainty; i++) {21                 do {22                     rng.GetBytes(bytes);23                     a = new BigInteger(bytes);24                 }25                 while (a < 2 || a >= source - 2);26 27                 BigInteger x = BigInteger.ModPow(a, d, source);28                 if (x == 1 || x == source - 1)29                     continue;30 31                 for (int r = 1; r < s; r++) {32                     x = BigInteger.ModPow(x, 2, source);33                     if (x == 1)34                         return false;35                     if (x == source - 1)36                         break;37                 }38 39                 if (x != source - 1)40                     return false;41             }42 43             return true;44         }

The above are my answers to this question. You are welcome to discuss and provide better solutions.

Code Stamp: files.cnblogs.com/tmywu/PrimeNumberProject.zip

 

 

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.