"Algorithm" general method and the method of filtering to find prime number

Source: Internet
Author: User

Prime number refers to the factor is only 1 and its own numbers (1 is not prime), the solution of prime numbers in mathematics is widely used, and the solution of the prime number within N is also our programming often encountered problems, in this problem, the screening method to solve the prime numbers are executed quickly. The following first describes how to infer a is not a prime number, and then introduce the ordinary method to find the prime number within N, followed by the filtering method to find the prime number of N, and finally the execution time of the two algorithms is relatively

Infer whether a number is prime

Algorithm thought: inferred that the sum of the squares of less than or equal to a number of all integers greater than 1 is divisible by this number, assuming that the number is not prime;

//infer whether a number is prime  bool  isplain (int  value )    {int  m = sqrt (value ); if  (value  < 2 ) return     false ; for  (int  i = 2 ; I <= m; i++) {if  ((value %i) ==0        ) {return  false ; }} return  true ;}  
Common method for solving primes within n

Algorithm idea: Declare an n-size bool array. The initial value is false, and then starts from 2 to infer whether a number is prime. If. Then the Boolean value is set to True

//普通法求素数bool* putong(int n){    boolvaluenewbool[n];    for (int0; i < n; i++)        valuefalse;    for (int2; i < n; i++){        if (isPlain(i)){            valuetrue;        }    }    returnvalue;}
The number of primes within n is calculated by filtering method

Algorithm idea: Find the prime number of the radical that is less than or equal to N. Then the multiples of all these primes in n are removed, the remaining numbers are primes, and are implemented by Boolean arrays

//filter method for prime numbersBOOL* Shuaixuan (intN) {BOOL*value=New BOOL[n]; for(inti =0; i<n; i++)value[I] =true;value[0] =false;value[1] =false; for(inti =2; I <= sqrt (n); i++) {if(value[i] && isplain (i)) {intc =2;intj = i*c; while(J < N) {value[j] =false;            j = i*c++; }        }    }return value;}
Complete code and execution results

The following code calls the normal method and the filter method, can loop input n (by CTRL + C end), for different data test, followed by a test to carry out a result diagram

#include <iostream>usingnamespace Std;#include <ctime>#include <math.h>#include <conio.h>//Infer whether a number is primeBOOLIsplain (int value){intm = sqrt (value);if(value<2)return false; for(inti =2; I <= m; i++) {if((value%i) = =0){return false; }    }return true;}//Common law seeking prime numbersBOOL* Putong (intN) {BOOL*value=New BOOL[n]; for(inti =0; I < n; i++)value[I] =false; for(inti =2; I < n; i++) {if(Isplain (i)) {value[I] =true; }    }return value;}//filter method for prime numbersBOOL* Shuaixuan (intN) {BOOL*value=New BOOL[n]; for(inti =0; i<n; i++)value[I] =true;value[0] =false;value[1] =false; for(inti =2; I <= sqrt (n); i++) {if(value[i] && isplain (i)) {intc =2;intj = i*c; while(J < N) {value[j] =false;            j = i*c++; }        }    }return value;}intMain () {intN while(CIN >> N) {intstart = Clock ();BOOL* value1 = Putong (n);intend = Clock (); cout <<"Normal method:"<< End-start << Endl; start = Clock ();BOOL* value2 = Shuaixuan (n);        end = Clock (); cout <<"Screening method:"<< End-start << Endl;        Delete[] value1;        value1 = NULL;        Delete[] value2;    value2 = NULL; } _getch ();}


It can be seen that the greater the N. The better the performance of the filter method is for the normal method.

"Algorithm" general method and the method of filtering to find prime number

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.