# Determination of prime numbers using cindram sieve

Source: Internet
Author: User
1. General Determination Method

The problem of prime number determination is to determine whether the given positive integer N is a prime number. A prime number refers to an integer with exactly two divisor numbers. Because N has no more than N, you only need to check 2 ~ Whether all Integers of N-1 divide n to determine whether it is a prime number. However, we can further optimize it. If D is an appointment of N, N/D is also an appointment of N. From n = D * n/d, min (d, n/D) is known, so you only need to check 2 ~ All integers are enough. In this case, the complexity of the prime number determination is O (). The code is implemented as follows:

`int classic(long long a){    long long half=(long long)sqrt(a);    for(int i=2;i<=half;i++)    {        if(a%i==0)  return 0;    }    return 1;}`

The general idea and implementation of prime number determination are very simple, but the efficiency is low. There are now many more efficient prime number determination methods, such as ferma testing, algorithms, and number field screening methods. However, these algorithms are either inaccurate or require too complicated mathematical knowledge, so we will not introduce them too much here. Next we will introduce a prime number determination method that only requires elementary mathematics to understand,This method provides twice the performance of conventional methods in any situation.

2 eshield screening method

Before introducing the new method, let's look back at the hereshield screening method, which is an algorithm as old as the moving phase division.

In the 3st century BC, the ancient Greek mathematician heratosini proposed a method to fabricate a prime number table (such as table 1 ). This method is similar to screening things. You can screen out unwanted things and leave what you need. The specific method is to arrange the natural numbers from 2 to N in sequence to 2, 3, 4, 5 ,..., N, then leave the first 2, and draw the multiples of all 2; the first number after 2 is 3, leave 3, and draw the multiples of all 3; the first number not marked after 3 is 5, leaving 5, and dividing all the multiples of 5; so proceed until there is no number to be marked in the preceding columns, all the prime numbers within N are left behind.

Table 1 eshield screening

If only one integer is used for the determination, the general determination method or the method described later is sufficient. However, if we want to determine the number of integers in a gender, we need to use the escret method. The following enumeration algorithms are taken from the challenge Programming Competition:

`Int prime [max_n]; bool is_prime [max_n]; // returns the number of prime numbers within n int sieve (int n) {int p = 0; For (INT I = 0; I <= N; I ++) is_prime [I] = true; is_prime  = is_prime  = false; For (INT I = 2; I <= N; I ++) {If (is_prime [I]) {Prime [p ++] = I; for (Int J = 2 * I; j <= N; J + = I) is_prime [J] = false ;}} return P ;}`
3. singaram screening

In 1934 AD, a young student in East India, singaram, proposed a screening method that is very different from that of heratosini. It first lists a table, such as table 2. Both the first row and the first column of the table are the 4-class equal deviation columns with a tolerance of 3. Starting from the second row, the next row is also an arithmetic deviation series with the tolerances of 5, 7, 9, 11, 13 ....... We can see that this table is actually a symmetric matrix.

Singaram points out: If n appears in the table, 2n + 1 is the sum. If n is not in the table, 2n + 1 is the prime number.. The proof is amazing!

Table 2 table constructed by singaram sieve

Verify the first half. First, he writes the first number of m rows:

4 + m-1) x 3 = 3 m + 1

Note that this row is an arithmetic difference sequence with a tolerance of 2 m + 1, so the number of column N in this row is:

3 m + 1 + (n-1) * (2 m + 1) = 2mn + m + n

That is, the number of n columns in Row M is 2mn + m + n. So 2n + 1 = 4mn + 2 m + 2n + 1 = (2 m + 1) (2n + 1) is the sum.

The second half. It is quite difficult to prove positive that 2n + 1 is a prime number. If it is replaced with an equivalent inverse negative proposition, it seems much easier to prove that "If 2n + 1 is not a prime number, then n must be in the table. In fact, if

2n + 1 = x * Y (X,Y)Is an integer

Then, because 2n + 1 is an odd number, X and Y must be an odd number. You may wish:

X = 2 p + 1; y = 2q + 1

So 2n + 1 = (2 p + 1) (2q + 1) = 4pq + 2 p + 2q + 1. Therefore, we can obtain

N = 2pq + p + q.

That is, n is the number of column Q in row P of the table.

In conclusion, we have proved the correctness of the singaram screening method. For example, if 18 is not in the table, 2*18 + 1 = 37 is a prime number. On the contrary, in the Table 71, 2*71 + 1 = 143 is the sum. The proof in the second part is an excellent example of the reverse verification method.

4. Determination of prime numbers based on sindham sieve

How can we determine whether a positive integer N is a prime number with the singaram screening method? We only need to judge whether (n-1)/2 is in the table. Since the number to be determined must be an odd number (even numbers must be a combination), (n-1)/2 can certainly be divisible. Determine whether the table exists, that is, determine whether p and q exist so that 2pq + p + q = (n-1)/2. The Code is as follows:

`int xindalamu(long long a){if(a%2==0) return 0;    long long aa=(a-1)>>1;    long long half=(long long)sqrt(aa/2);    for(int i=1;i<=half;i++)    {        if((aa-i)%(2*i+1)==0) return 0;    }    return 1;}`

The above code is explained in detail below. First, determine whether the user input is an even number. If the user input is an even number, false is returned directly. Then, the user input is subtracted by one divided by two and assigned to AA. Then, you need to determine whether AA is in the table. The judgment method is similar to the conventional judgment method. It also needs to verify whether a row has been divisible. However, the formula here is slightly different. From the code above, we can see that when we open the AA, we divide it by two. Why? Determine whether AA appears in the table, that is, determine whether p and q exist so that 2pq + p + q = AA. This is slightly different from the conventional method in determining AA = p * q. If two small items are ignored, we need to determine whether 2pq is equal to AA, that is, whether PQ is equal to AA/2. This is the reason for dividing by 2. The for loop checks from the first row to determine whether the row has a column 2pq + p + q = AA. When the row number is fixed, the method to determine is to check whether AA-P is divided by 2 p + 1 (a slight change can be seen ).

Let's analyze the complexity of determining prime numbers based on the sindham sieve. From the for loop, we can see that the complexity is O (), but the constant coefficient is smaller than the conventional method. This is because there is an operation divided by two in the request for AA, and there is another operation divided by two in the request, so it is equivalent to opening a/4, therefore, you only need to judge the/2 rows at most. The conventional method requires verification of a maximum of different factors, so it can be seen that the prime number determination method based on the Cinda ramper screen is twice faster than the conventional method.

However, there is another question to be solved. If the user inputs a sum, whether the method based on the prime number of the cindham sieve is better than the conventional method is used. It can prove that,No matter whether the input is a combination of numbers or a prime number, the prime number determination method based on the sindram sieve is always twice faster than the conventional method.The proof is as follows:

If the user inputs a prime number, the complexity of the analysis has been proved. If the user inputs a unionMinimum prime factor BReturns, and the cindram determination method returns when traversing to the second (b-1)/2 row. Suppose a = B * y, because a is a combination, there must be P row and Q column so 2pq + p + q = (A-1)/2. (2 p + 1) (2q + 1) =. Because the QPS determination method starts from the first line, we can consider B = 2 p + 1, thus the QPS DETERMINATION METHOD IN THE (b-1) if the input value is an odd number, the prime number determination method based on the sindham sieve is twice faster than that of the conventional method.

5. Prime Number screening based on sindham sieve

The initial purpose of the singaram sieve is the same as that of the heratosane sieve. It is also the filtering of all prime numbers. The above determination of prime numbers is a deformation application. filtering all prime numbers is the most important purpose of the sieve. However, compared with the hereshield screening method, singdalams screening is not so easy to obtain all prime numbers. Conclusion obtained from sindalams:If n appears in the table, 2n + 1 is the sum. If n is not in the table, 2n + 1 is the prime number., We can easily know that not all the numbers in the table, the second double plus contains all the odd prime numbers. For example, the first 10 numbers in a table are 4, 7, 10, 12, 13, 16, 17, 19, and 24. The numbers that are not in the table are 1, 2, 3, 5, 6, 8, 9, 11, 15, 18, 20, 23, the two times of these numbers are respectively 3, 5, 7, 11, 13, 17, 29, 31, 37, 41, 47, that is, the first 14 odd prime numbers. Although we only need to find all the numbers not in the table to obtain all the odd prime numbers, how to find them is a troublesome problem. The problem is how to traverse all the numbers in the table in order. I have no idea for the time being. I would like to thank you for your help.

My understanding of singaram screening was obtained from a high school mathematics exercise set. At that time, I was very impressed with the expanded reading after the exercises, so that I have kept the exercise set until now, thanks.
Related Keywords:

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.