[Classic algorithm] Eratosthenes screening to find prime numbers

Source: Internet
Author: User

Title Description:

In addition to itself, the number that cannot be divisible by other integers is called prime, and it is simple to ask for prime numbers, but how to quickly find prime numbers has always been a task for programmers and mathematicians, and here we introduce a famous Eratosthenes for prime number method.

Topic Analysis:

First you know that this problem can be solved by using a loop, dividing a specified number by all numbers less than it, and if divisible is not prime, but how to reduce the number of check loops? How do I find all prime numbers that are less than n?
First of all, assuming that the number to check is n good, then in fact, as long as the check to the open root of n can be, the reason is very simple, assuming that a*b = N, if a is greater than the open root of N, then in fact, before the check is less than a can be checked to B this number can be divisible by N. However, the use of the open root in the program is a matter of accuracy, so you can use I*i <= N to check and execute faster.
Again, suppose there is a sieve to store 1~n, for example:

2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 ... N

First sieve the multiples of 2:

2 3 5 7 9 11 13 15 17 19 21 ..... N

Then sieve the multiples of 3:

2 3 5 7 11 13 17 19 ..... N

Then sift through the multiples of 5, then sift the 7 prime numbers, then sift the multiples of 11 .... So the last number left is prime, and this is the Eratosthenes screening method (Eratosthenes Sieve).
The number of checks can also be reduced, in fact, as long as the check 6n+1 and 6n+5 can be, that is, directly skip the multiples of 2 and 3, so that the program if the check action can be reduced.

Program code:
#include <iostream>#include<vector>#include<algorithm>#include<gtest/gtest.h>using namespaceStd;vector<int> Eratosthenesprime (intnSize) {    BOOL* Primes =New BOOL[NSize]; memset (Primes,true,sizeof(BOOL)*nSize);  for(intI=2; I * I <= nSize; ++i) {if(Primes[i]) { for(intj = i*i; J < NSize; j+=i) {primes[j]=false; }}} vector<int>Result;  for(intI=2; i < nSize; ++i) {if(Primes[i]) {result.push_back (i); }    }    Delete[] Primes; returnResult;} TEST (Algo, teratosthenesprime) {vector<int> Result = Eratosthenesprime ( -); cout<<"n:100"<< result.size () <<Endl;  for(vector<int>::size_type i=0; I<result.size (); ++i) {ifI -==0) cout<<Endl; cout<< Result[i] <<" "; } cout<< Endl <<Endl; Result= Eratosthenesprime ( -); cout<<"n:500"<< result.size () <<Endl;  for(vector<int>::size_type i=0; I<result.size (); ++i) {ifI -==0) cout<<Endl; cout<< Result[i] <<" "; } cout<< Endl <<Endl; Result= Eratosthenesprime ( +); cout<<"n:1000"<< result.size () <<Endl;  for(vector<int>::size_type i=0; I<result.size (); ++i) {ifI -==0) cout<<Endl; cout<< Result[i] <<" "; } cout<< Endl <<Endl;}

Reference:

Https://en.wikipedia.org/wiki/Sieve_of_Eratosthenes

[Classic algorithm] Eratosthenes screening to find prime numbers

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.