Eratosthenes screening method, eratosthenes screening

Source: Internet
Author: User

Eratosthenes screening method, eratosthenes screening

Write down all integers from 2 to a certain upper limit. In the remainder part of the algorithm, you traverse the entire list and remove all integers that are not prime numbers.

Find the first unexcluded INTEGER (that is, 2) in the list, and then remove all the numbers next to the list because they can be divisible by 2. Then, return to the list header and start again. At this time, the first element in the list that has not been removed is 3. Therefore, after 3, the number of elements is removed from the list, and then return to the beginning of the List, and so on, all excluded items in the final list are prime numbers.

First, consider implementing the simplest such function. Next we will improve this algorithm:

 

1 # include <stdio. h> 2 # include <stdlib. h> 3 # define SIZE 1000 4 # define FALSE 0 5 # define TRUE 1 6 7 int 8 main () 9 {10 char sieve [SIZE]; // filter array 11 char * sp; 12 int number; // used to indicate the number 13 int sum = 0; 14 15 for (sp = & sieve [0]; sp <& sieve [SIZE]; sp ++) // Initialize all values in the array to TRUE 16 * sp = TRUE; 17 18 for (number = 2; number ++) // within the loop, set exit loop 19 {20 sp = & sieve [0] + number-2; // point the pointer to the first number 21 if (sp >=& sieve [SIZE]) 22 break; 23 while (sp + = number, sp <& sieve [SIZE]) // jump back to the number and set it to 024 {25 * sp = FALSE; 26} 27} 28 for (number = 2, sp = & sieve [0]; sp <& sieve [SIZE]; number ++, sp ++) // 29 {30 if (* sp) 31 {32 sum ++; 33 printf ("% d \ t", number); 34} 35 if (sum % 5 = 0) 36 printf ("\ n "); 37} 38 printf ("the total of count is: % d", sum); 39 40}

In this way, there are 1000 prime numbers in 168.

This algorithm needs to filter the first number of arrays as the prime number, and 2 is used in the code above. In fact, we can save a little space because even numbers are not prime numbers except 2, so we can start with the odd number directly, so that the range we can find is doubled. Just change the code above:

1 # include <stdio. h> 2 # include <stdlib. h> 3 # define SIZE 1000 4 # define FALSE 0 5 # define TRUE 1 6 7 int 8 main () 9 {10 char sieve [SIZE]; 11 char * sp; 12 int number; 13 int sum = 0; 14 15 for (sp = & sieve [0]; sp <& sieve [SIZE]; sp ++) 16 * sp = TRUE; 17 18 for (number = 3; number + = 2) // change the initial value and traverse the odd number 19 {20 sp = & sieve [0] + (number-3)/2; // note that the first number is 21 if (sp >=& sieve [SIZE]) 22 break; 23 while (sp + = number, sp <& sieve [SIZE]) 24 {25 * sp = FALSE; 26} 27} 28 for (number = 3, sp = & sieve [0]; sp <& sieve [SIZE]; number + = 2, sp ++) // pay attention to the change in the cyclic Volume 29 {
Printf ("2 \ n"); // 2 is also the prime number 30 if (* sp) 31 {32 sum ++; 33 printf ("% d \ t ", number); 34} 35 if (sum % 5 = 0) 36 printf ("\ n"); 37} 38 printf ("the total of count is: % d ", + + sum); // 2 is also the prime number 39 40}

In this way, we can obtain 2000 prime numbers within 303.

Related Article

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.