]]>Some algorithms about judging a prime .http://www.shnenglu.com/cxl82116/archive/2007/04/19/22266.html灝忛緳鍝?/dc:creator>灝忛緳鍝?/author>Wed, 18 Apr 2007 19:05:00 GMThttp://www.shnenglu.com/cxl82116/archive/2007/04/19/22266.htmlhttp://www.shnenglu.com/cxl82116/comments/22266.htmlhttp://www.shnenglu.com/cxl82116/archive/2007/04/19/22266.html#Feedback6http://www.shnenglu.com/cxl82116/comments/commentRss/22266.htmlhttp://www.shnenglu.com/cxl82116/services/trackbacks/22266.html 1.GRIDDLE METHOD (ALSO CALLED SIFT METHOD)
When I was a student in Bachelor phrase , a teacher has tought me a method called griddle method , it's principle is:
if a number can be devided by another number(except 1) , it isn't a prime , so , we set the non-prime at zero. after all number [In fact , half of the range checked is OK ]test finished , We simply output the NON-ZERO number , it 's the prime table in the RANGE.
Here is the Kernel Function(Quote : STL TURORIAL REFERRENCE):
1//predicate, which returns whether an integer is a prime number 2bool isPrime (int number) 3{ 4//ignore negative sign 5number = abs(number); 6// 0 and 1 are prime numbers 7if (number ==0|| number ==1) { 8returntrue; 9} 10//find divisor that divides without a remainder 11int divisor; 12for (divisor = number/2; number%divisor !=0; --divisor) { 13; 14} 15//if no divisor greater than 1 is found, it is a prime number 16return divisor ==1; 17}
In Main Function , traverse the given range judge every number use the above function:
int main(int argc , char* argv[]) { int A[100]; InitArray(A,100); for(int i=0;i<100;i++) if(isPrime(A[i])) cout<<A[i]<<endl; }
3. Extention Further , if there is a given List or Vector and it's filled with data , how can you find the prime number in the data effiectly ? STL Algorithm can help you indeed. After the step two , we can write a few code to implement the function:
int main() { list<int> coll; //insert elements from 1 to 100 for (int i=1; i<=100; ++i) { coll.push_back(i); } //search for prime number list<int>::iterator pos; pos = find_if (coll.begin(), coll.end(), //range isPrime); //predicate if (pos != coll.end()) { //found cout <<*pos <<" is first prime number found"<< endl; } else{ //not found cout <<"no prime number found"<< endl; } }
]]>How can you efficeny judge whether the num is primer?http://www.shnenglu.com/cxl82116/archive/2007/04/19/22265.html灝忛緳鍝?/dc:creator>灝忛緳鍝?/author>Wed, 18 Apr 2007 18:39:00 GMThttp://www.shnenglu.com/cxl82116/archive/2007/04/19/22265.htmlhttp://www.shnenglu.com/cxl82116/comments/22265.htmlhttp://www.shnenglu.com/cxl82116/archive/2007/04/19/22265.html#Feedback0http://www.shnenglu.com/cxl82116/comments/commentRss/22265.htmlhttp://www.shnenglu.com/cxl82116/services/trackbacks/22265.html There is a easy way to do it , the follow code isn't written by me but a classic method
E.G quote from STL tutorial reference
#include <iostream> #include <list> #include <algorithm> #include <cstdlib>//for abs() usingnamespace std; //predicate, which returns whether an integer is a prime number bool isPrime (int number) { //ignore negative sign number = abs(number); // 0 and 1 are prime numbers if (number ==0|| number ==1) { returntrue; } //find divisor that divides without a remainder int divisor; for (divisor = number/2; number%divisor !=0; --divisor) { ; } //if no divisor greater than 1 is found, it is a prime number return divisor ==1; }