Prime screening algorithm

Source: Internet
Author: User

<1> method 1

// Determine whether it is a prime number int isprime (int A) {//, negative numbers are non-prime numbers if (a <= 1) {return 0 ;}// calculate the upper bound of enumeration, to prevent the loss of precision caused by the double value, the root number is used to get an integer and Add 1. That is, you would rather enumerate one more number than int bound = (INT) SQRT (A) + 1; for (INT I = 2; I <bound; I ++) {// enumerate whether these numbers can be divided by X, if yes, it is not a prime number. If (a % I = 0) {return 0 ;}} return 1 ;}

<2> method 2

# Define maxsize 10001int mark [maxsize]; int prime [maxsize]; // judge whether it is a prime number mark array index prime number int prime () {int Index = 0; memset (mark, 0, sizeof (Mark); For (INT I = 0; I <maxsize; I ++) {// If (MARK [I] = 1) {continue;} else {// otherwise, a prime [index ++] = I; // The multiple that mark this prime number is not a prime number for (Int J = I * I; j <maxsize; j + = I) {mark [J] = 1 ;}}} return Index ;}

<3> method 3

This method is easy to understand. In the initial stage, assume that all values are prime numbers. When a prime number is found, it is clear that the prime number is a combination after the other number.

All these operators are screened out, that is, the origin of the algorithm name. However, careful analysis shows that this method will cause repeated sieve division, affecting efficiency.

For example, 10 is screened once at I = 2, K = 2*15, and again at I = 5, K = 5*6. Therefore, there is a fast linear screening method.

Int mark [maxsize]; int prime [maxsize]; // judge whether it is a prime number mark array index prime number int prime () {int Index = 0; memset (mark, 0, sizeof (Mark); For (INT I = 2; I <maxsize; I ++) {// if not marked, a prime number is obtained. If (MARK [I] = 0) {Prime [index ++] = I;} // I times the current prime number is not a prime number for (Int J = 0; j <index & prime [J] * I <maxsize; j ++) {mark [I * prime [J] = 1; if (I % prime [J] = 0) {break ;}} Return Index ;}

Each sum must have a minimum prime factor. Each sum is screened exactly once by its smallest prime factor. So it is linear time.
Code:
If (I % prime [J] = 0) break;
The prime number in the prime array increases progressively. When I can divide prime [J], the sum of I * prime [J + 1] Must be filtered out by prime [J] multiplied by a certain number.
Because I contains prime [J], prime [J] is smaller than prime [J + 1. The next prime number is the same. So you don't need to screen it down.
Before meeting the I % prme [J] = 0 condition and when the first condition is met, PR [J] must be the minimum factor of PR [J] * I.

Refer to blog: Click to open the link

Exercise: Click to open the link

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.