I just saw a question.
Http://topic.csdn.net/u/20100227/14/cdb02e8f-c08b-4bcb-a44c-e8d4c9ab95da.html
Use C or C ++ to write the following programs that comply with POSIX specifications.
Write a program that lists all the prime numbers that long can represent.
Thread a is required to use the Rabin-Miller Algorithm for screening, and thread B is verified by algorithm 2.
After thread a screens the number of n (n = 100) (thread B should assist thread A in this period ),
Thread B starts verification, thread a continues to screen,
After thread a completes the number of screens, it will assist in line B verification.
A prime number is an integer that represents the product of itself and 1, regardless of whether it represents the product of any two integers.
Prime Number algorithm:
1: Start from 2 and use the "yes, leave, or remove" method to list all the columns (until you do not want to do the following, for example, until 10,000 ). The first number is 2, which is a prime number, so we should keep it, and then continue to count, delete a number every other number, in this way, all the numbers that can be divisible by 2 and thus not prime numbers can be removed. Among the smallest number left, 3 is next to 2, which is the second prime number. Therefore, we should leave it, and then delete one number every two numbers starting from it, in this way, all the numbers that can be divisible by 3 can be removed. The number not removed next is 5, and then one number is deleted at intervals of 4 to remove all the numbers that can be divisible by 5. The next number is 7, the next number is deleted at intervals of 6, the next number is 11, And the next number is deleted at intervals of 10. The next number is 13, and the next number is deleted at intervals of 12. In this way, we will proceed in accordance with the law.
However, we generally do not use the above method for programming. In other words, this method cannot be implemented by computers, or the implementation algorithm is complicated. Because it is more like a mathematical reasoning. The following are common algorithms used:
Set n = 2 ^ 127-1 to a 38-digit number. to verify whether it is a prime number, there are several different methods:
1. Traverse each integer below the square root of N greater than 2. Can n be divisible? (this is the most basic method)
2. Traverse each prime number below the square root of N above 2 and check whether N can be divisible. (This method is an improvement of the above method, but all the prime numbers below the square root of N are required)
(The following is used for the length)
3. Use the Rabin-Miller algorithm (this algorithm emerged in 1978, which is the first algorithm that can be used for both data encryption and digital signature. It is easy to understand and operate, and is also popular. The algorithm is named by the inventor Ron Rivest, adishamir, and Leonard Adleman. However, the security of RSA has never been proved theoretically. The security of RSA depends on the fact that it is difficult to break down large numbers .);
4. AKs algorithm (Agrawal, kayal, and Saxena );
Calculation Result: assuming that the computer can calculate 0.1 billion division times per second
Algorithm 1 takes 4136, algorithm 2 takes 93 years, and algorithm 3 takes less than 1 second!
The Rabin-Miller algorithm is a typical method to verify whether a number is a prime number. The method for determining prime numbers is the Rabin-Miller probability test. What is his specific process. If we want to determine whether N is a prime number, we must first ensure that N is an odd number, then we can represent N as n = (2 ^ r) * s + 1, note that s must also be an odd number. Then we need to select a random integer A (1 <= A <= N-1). Next we need to judge a ^ s = 1 (mod N) or a ^ (2 ^ J) * S) =-1 (mod n) (0 <= J if any one of them is true, we say n has passed the test, however, it may not be a prime number that can be tested. Therefore, we usually need to perform such tests multiple times to ensure that we get a prime number. (The DDs standard requires 50 tests)
Use the Rabin-Miller Algorithm for checking
First, select a random number P for the test generation, and calculate the number of times B and B are 2 in the total division of p-1. Then calculate m so that n = 1 + (2 ^ B) M.
(1) Select a random number a less than P.
(2) Set J = 0 and Z = a ^ m mod p
(3) If Z = 1 or Z = p-1, then P passes the test and may make the prime number
(4) If j> 0 and Z = 1, P is not a prime number.
(5) set J = J + 1. If J and z <> PM, set Z = Z ^ 2 mod P and return to (4 ). If Z is p, p passes the test and may be a prime number.
(6) If J = B and z <P-1, it is not a prime number.