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

Source: Internet
Author: User

The 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 very extensive, and the solution of the prime number within N is also the problem we often encountered in programming, in this problem, the screening method for solving prime numbers runs very fast. The following first describes how to determine whether a prime number, and then introduce the ordinary method to find the prime number within N, followed by the filter method to find the prime number of N, and finally the two algorithms running time comparison

Determine if a number is prime

Algorithm thought: Judging the sum of squares of less than or equal to a number of all integers greater than 1 is not divisible by this number, if so, it indicates that the number is not a prime; conversely, it is prime.

//determine if 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 start from 2 to determine whether a number is a prime, if so, its 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, and then remove all the multiples of these primes in N, the remaining numbers are prime numbers 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 running results

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

#include <iostream>usingnamespace Std;#include <ctime>#include <math.h>#include <conio.h>//Determine if 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 larger the N, the better the filtering method will be 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.