Prime Number and kernel Testing

Source: Internet
Author: User

A number is a prime number (also called prime number). if and only when its approximate number has two -- 1 and itself. It is specified that the two approx. numbers cannot be the same, so 1 is not a prime number. The Study of prime numbers belongs to the category of number theory. You can see that many mathematicians come up with some prime numbers that match certain properties and call them a prime number. The entire number theory is almost switched around the word division and prime number. For code writers, prime numbers are more important than imagined. if you Google bigprime or big_prime, you will always find a bunch of program codes using prime number constants. You can remember some prime numbers for urgent use. I will select some memorable prime numbers, such as 4567,124 567, 3214567,234 56789, 55566677,123 4567894987654321, 11111111111111111111111 (23 1 ). The first 10 digits of my mobile phone number are prime numbers. The ASCII code of my website domain name is also a prime number (77 97 116 114 105 120 111 54 55 46 99 109. In addition, the eighth birthday of a certain mm of mine is also a prime number. Every time I need a bigprime constant to write something like a hash function, I will take her birthday and hope she can bring me good luck. Sometimes I call her su mm. No one knows what it means, and she doesn't know what it means.
Prime numbers have many magical properties. I will write 5 below for your appreciation.

1. The number of prime numbers is infinite (the maximum prime number does not exist)
Proof: if there is the largest prime number P, we can construct a new number 2*3*5*7 *... * p + 1 (all prime numbers multiply by 1 ). Obviously, this number cannot be divisible by any prime number (all prime numbers except it all have more than 1), which indicates that we have found a larger prime number.

2. There is a continuous number of any long segments, where all the numbers are the Union (the interval between adjacent prime numbers is arbitrary)
Proof: When 0 <A <= n, n! + A can be divisible by. The number of N series with the length of N-1! + 2, n! + 3, n! + 4,..., n! + In N, all numbers are the sum. This conclusion is true for all integers n greater than 1, and N can be any larger.

3. All prime numbers greater than 2 can uniquely represent the difference between two integers.
Proof: the prime numbers greater than 2 are odd. Assume that the number is 2n + 1. Because (n + 1) ^ 2 = n ^ 2 + 2n + 1, (n + 1) ^ 2 and N ^ 2 are the two workers we are looking. The following shows that this solution is unique. If the prime number P can be expressed as a ^ 2-B ^ 2, P = a ^ 2-B ^ 2 = (a + B) (a-B ). Since P is a prime number, only a + B = P and a-B = 1 are possible. This gives the unique solutions of A and B.

4. When n is an integer greater than 2, if one of the numbers 2 ^ n + 1 and 2 ^ N-1 is a prime number, the other must be a union.
Proof: 2 ^ n cannot be divisible by 3. If it is divided by 3, then 2 ^ n-1 can be divisible by 3; if it is divided by 3, then 2 ^ n + 1 can be divisible by 3. In short, at least one of 2 ^ n + 1 and 2 ^ N-1 is a combination.

5. If P is a prime number and A is a positive integer smaller than P, A ^ (p-1) mod p = 1.
This proves to be a little troublesome.
First, we prove the conclusion that if p is a prime number, then any positive integer less than P, A, 2a, 3A ,..., (p-1) the remainder of a divided by P is exactly a one-P-1 arrangement. For example, 5 is a prime number, and the remainder of 3, 6, 9, 12 divided by 5 is 3, 1, 4, 2, which is exactly the four numbers from 1 to 4.
If the conclusion is not true, there are two positive integers m and n smaller than P, so that the remainder of NA and Ma divided by P is the same. Assume n> m, then P can divide a (n-m ). However, P is a prime number, so at least one of A and n-M contains the factor p. This is obviously impossible, because a and N-m are smaller than P.
UseSame formulaWe have proved that:
P-1 )! When a * 2a * 3A *... * (p-1) A (mod P)
That is:
P-1 )! Bytes (p-1 )! * A ^ (p-1) (mod P)
Both sides are divided by P-1 )!, The final conclusion is as follows:
1 then a ^ (p-1) (mod P)

Unfortunately, I did not prove this theorem at first. This is proved by a great mathematician Fermat, called Fermat's little theorem ). Euler extends this theorem, which is called Euler's theorem. There are too many Theorem in Euler's life. to distinguish it from other "Euler's Theorem", it is called the Euler extension of the Fermat theorem. In the Euler's theorem, a function f (m) is used to represent the number of integers smaller than M and M (the two numbers have only a common approx. 1 is called an interol ). For convenience, we usually use the mark (m) to represent this function (known as the Euler function ). Euler pointed out that if a and m are mutually Prime, then a ^ PHI (m) 1_1 (mod m ). We can see that when M is a prime number, Phi (m) is equal to m-1 (all positive integers smaller than m are mutually prime with m), so it is a promotion of Fermat's theorem. The proof of the theorem is almost the same as that of the Fermat small theorem, but the formula to be considered becomes the product of all the numbers of M elements: M_1 * M_2... M _ PHI (m) round (A * M_1) (A * M_2 )... (A * M _ PHI (M) (mod m ). Why should I mention the Euler's theorem by the way? The following statement adds the PV of my website: this theorem appears.The hundred greatest Theorems.

Speaking of the Fermat theorem, there are many misunderstandings in the history of mathematics. For a long time, people thought that the inverse proposition of the Fermat theorem was correct, and someone personally verified all the situations where a = 2, P <300. There is even a saying that China proved this theorem in the Confucius era: If n is divided by 2 ^ (n-1)-1, n is the prime number. Later, a British scholar made a research and found that it was because they made a mistake when translating ancient Chinese documents. In 340, someone discovered the first inverse example of the inverse proposition of the Fermat small theorem: although the power of 2 is divided by more than 341 1, 341 = 11*31. Later, it was discovered that the inverse proposition of the Fermat theorem at the time of a = 2 was not true even when the number of 561,645,110 equals. Although there are not many such numbers, they cannot be ignored. Therefore, we call all the Union numbers that can divide 2 ^ (n-1)-1 into Pseudo Prime, which means that this prime number is false.
N that does not satisfy 2 ^ (n-1) mod n = 1 must not be a prime number; If yes, it is mostly a prime number. In this way, a more efficient method for determining the prime number of elements than the Trial Division appears: to create a Pseudo Prime Number table and record all Pseudo Prime numbers within a certain range, then all values meet the requirements of 2 ^ (n-1) MOD n = 1 and N is not in the Pseudo Prime Number table. This method is faster because we can use the binary method to quickly calculate the value of 2 ^ (n-1) mod N, which is very easy with the help of computers; in the computer, you can also use binary search for ordered series, hash table to split the hash, and trie tree to improve the efficiency of searching Pseudo Prime numbers.
Someone naturally cares about the question: how many Pseudo Prime numbers are there? In other words, if I only calculate the value of 2 ^ (n-1) mod n and do not prepare a Pseudo Prime Number table in advance, then how many probabilities of errors will be judged by gender? It is very valuable to study this problem. After all, we are oier and it is impossible to carry a constant array of thousands of characters to the test room. Statistics show that there are a total of 1 billion prime numbers in the first 50847534 natural numbers, and there are 5597 total number n that meet 2 ^ (n-1) mod n = 1. In this case, the probability of an algorithm error is about 0.00011. This probability is too high. If you want to avoid creating a pseudo-prime number table, we need to improve the algorithm for determining the prime number.

The simplest idea is that we only considered the case of a = 2. For the expression a ^ (n-1) mod N, different a may lead to different results. A sum may pass the test when a = 2, but the calculation result when a = 3 does exclude the possibility of prime numbers. Therefore, the definition of Pseudo Prime numbers has been extended, and the number n that satisfies a ^ (n-1) mod n = 1 is called the Pseudo Prime Number (Pseudo Prime to base a) based on ). In the first 1 billion natural numbers, there are only 1272 Pseudo Prime numbers based on 2 and 3 at the same time, which is less than 1/4. This tells us that if both a = 2 and a = 3 are verified, the probability of an algorithm error is reduced to 0.000025. It is easy to think that the more a is used for testing, the more accurate the algorithm is. Generally, we randomly select a number of positive integers smaller than the number to be tested as the base number A for several tests. If one test fails, we immediately throw the number back to the combined world. This is the Fermat test.
People will naturally wonder if the probability of errors can be reduced to 0 if all the bases less than n a are considered? Unexpectedly, there is such a combination. It can pass all a tests (this statement is not accurate. For details, see my reply on the ground core Floor ). Carmichael was the first to discover such extreme Pseudo Prime numbers, which he called Carmichael numbers. You will surely think that such a number must be large. Error. The number of the first Carmichael is surprisingly small, just a three-digit number, 561. Among the first 1 billion natural numbers, there are also as many as 600 Carmichael numbers. The existence of Carmichael number indicates that we need to continue to strengthen the algorithm for determining the number of elements.

The work of Miller and Rabin has taken a revolutionary step in the Fermat test and established the legendary Miller-Rabin algorithm. The new test is based on the following theorem: If P is a prime number, X is a positive integer smaller than P, and x ^ 2 mod p = 1, then either x = 1 or X = p-1. This is obvious, because x ^ 2 mod p = 1 is equivalent to P which can divide x ^ 2-1, that is, P can divide (x + 1) (x-1 ). Since P is a prime number, it is only possible that the X-1 can be divisible by P (at this time x = 1) or x + 1 can be divisible by P (at this time x = PM ).
The following example shows how the theorem is applied to the Fermat test. As mentioned above, 341 can pass a 2-based Fermat test, because 2 ^ 340 mod 341 = 1. If 341 is a prime number, 2 ^ 170 mod 341 may only be 1 or 340. If 2 ^ 170 mod 341 is equal to 1, we can continue to view the result of dividing 2 ^ 85 by 341. We found that 2 ^ 85 mod 341 = 32, this result removed the prime crown on the head of 341, and the real face behind the mask appeared, the attempt to pretend to be a prime number to interact with our plain mm was revealed.
This is Miller-Rabin's method for the testing of the kernel. Continuously extract factor 2 from the exponent N-1 and express n-1 as D * 2 ^ r (where D is an odd number ). What we need to calculate is the remainder of D * 2 ^ r of a divided by N. So a ^ (D * 2 ^ (r-1) is either equal to 1 or n-1. If a ^ (D * 2 ^ (r-1) is equal to 1, the theorem continues to apply to a ^ (D * 2 ^ (R-2, the A ^ d mod n = 1 or N-1 is obtained after an I satisfies a ^ (D * 2 ^ I) mod n = n-1 or 2 in the final index. In this way, the Fermat theorem is enhanced in the following form:
Extract factor 2 as much as possible and express n-1 as D * 2 ^ r. If n is a prime number, or a ^ d mod n = 1, or there is an I so that a ^ (D * 2 ^ I) mod n = N-1 (0 <= I <R) (note that I can be equal to 0, this will unify the situation of a ^ d mod n = n-1 to the end)
Miller-Rabin is also an uncertain algorithm. We call the sum of Miller-Rabin tests based on A as the base-strong Pseudo Prime (strong Pseudo Prime ). The first base-2 strong Pseudo Prime Number is 2047. The first strong Pseudo Prime Number base on 2 and 3 is as large as 1 373 653.
The code of the Miller-Rabin algorithm is also very simple: Calculate the values of D and R (which can be accelerated by bitwise operations), and then calculate the values of a ^ d mod N in two parts, finally, it is squared to R times. The code of the program is simpler than imagined. I write a copy and put it below. Although I have already switched to C, I believe there are still many people who do not understand C. Let me write Pascal again. The isprime function returns whether the specified base number A and N can pass the test. If the function returns false, n is not a prime number. If the function returns true, n is very likely a prime number.Note that the data range of this Code is limited to longint. You may need to change them to int64 or high-precision computing.
function pow( a, d, n:longint ):longint;
begin
   if d=0 then exit(1)
   else if d=1 then exit(a)
   else if d and 1=0 then exit( pow( a*a mod n, d div 2, n) mod n)
   else exit( (pow( a*a mod n, d div 2, n) * a) mod n);
end;

function IsPrime( a,n:longint ):boolean;
var
   d,t:longint;
begin
   if n=2 then exit(true);
   if (n=1) or (n and 1=0) then exit(false);
   d:=n-1;
   while d and 1=0 do d:=d shr 1;
   t:=pow( a, d, n );
   while ( d<>n-1 ) and ( t<>1 ) and ( t<>n-1 ) do
   begin
      t:=(t * t)mod n;
      d:=d shl 1;
   end;
   exit( (t=n-1) or (d and 1=1) );
end;

Miller-Rabin is currently the most widely used algorithm for the determination of large numbers. Generally, the base number is still randomly selected, but when the number of tests is not large, there are some tips to select the base number. For example, if the number to be tested is less than 4 759 123 141, you only need to test three bases 2, 7, and 61. Of course, the more you test, the larger the correct range. If you use the first seven prime numbers (2, 3, 5, 7, 11, 13, and 17) each time, all the numbers that do not exceed 341 550 071 728 320 are correct. If the base number is 2, 3, 7, 61, and 24251, the unique strong Pseudo Prime Number in 10 ^ 16 is 46 856 248 255 981. These conclusions make the Miller-Rabin algorithm very useful in Oi. It is generally believed that the accuracy rate of Miller-Rabin testing is acceptable. The loss rate of the random selection of K bases for testing is approximately 4 ^ (-K ).

The Miller-Rabin algorithm is an RP algorithm. RP is a kind of time complexity, mainly for the determination problem. One algorithm is the RP algorithm, which indicates that it can be completed in polynomial time and can accurately judge the case where the answer is no, but at the same time, it may also make the right sentence wrong (the error probability cannot exceed 1/2 ). The RP algorithm is based on randomization, so running this algorithm multiple times can reduce the error rate. There are other probabilistic testing algorithms, such as the solovay-strassen algorithm. In addition, some native testing algorithms need to know some auxiliary information (such as n-1 prime factor) in advance, or the number to be tested must meet certain conditions (for example, the number to be tested must be in the form of 2 ^ N-1 ). The AKs algorithm has made a sensation in the world over the past few years. It is the first Polynomial-based, deterministic, and non-conditional prime judgment algorithm. At that time, a paper was published with the title primes is in P, and the whole world went crazy. There were several mm in our class that came to the initial tide. The algorithm is mainly based on the following fact: N is a prime number when and only when (X-a) ^ n squared (x ^ N-A) (mod N ). Note that X is an unknown number in the polynomial, and each side of the equation is a polynomial. For example, when a = 1, the proposition is equivalent to the following conclusion: WHEN n is a prime number, the n + 1 line in the Yang Hui triangle can be divisible by N except the 1 at both ends.

Matrix67 original
Please indicate the source of the post

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.