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.
E.G
Define the Range from 1-100;
/********************************************************************
created: 2007/04/19
created: 19:4:2007 3:00
filename: C:\testvc6\TestStll\TestStll.cpp
file path: C:\testvc6\TestStll
file base: TestStll
file ext: cpp
author: Chang xinglong(King.C)
purpose: Print Prime Table in RANGE(1-100)
*********************************************************************/
created: 2007/04/19
created: 19:4:2007 3:00
filename: C:\testvc6\TestStll\TestStll.cpp
file path: C:\testvc6\TestStll
file base: TestStll
file ext: cpp
author: Chang xinglong(King.C)
purpose: Print Prime Table in RANGE(1-100)
*********************************************************************/
The Code Here :































































2.THE DIRECT METHOD
E.G
/********************************************************************
created: 2007/04/19
created: 19:4:2007 3:00
filename: C:\testvc6\TestStll\TestStll.cpp
file path: C:\testvc6\TestStll
file base: TestStll
file ext: cpp
author: Chang xinglong(King.C)
purpose: Prime ?
*********************************************************************/
created: 2007/04/19
created: 19:4:2007 3:00
filename: C:\testvc6\TestStll\TestStll.cpp
file path: C:\testvc6\TestStll
file base: TestStll
file ext: cpp
author: Chang xinglong(King.C)
purpose: Prime ?
*********************************************************************/
Here is the Kernel Function(Quote : STL TURORIAL REFERRENCE):
1
//predicate, which returns whether an integer is a prime number
2
bool isPrime (int number)
3

{
4
//ignore negative sign
5
number = abs(number);
6
// 0 and 1 are prime numbers
7
if (number == 0 || number == 1)
{
8
return true;
9
}
10
//find divisor that divides without a remainder
11
int divisor;
12
for (divisor = number/2; number%divisor != 0; --divisor)
{
13
;
14
}
15
//if no divisor greater than 1 is found, it is a prime number
16
return divisor == 1;
17
}

2

3



4

5

6

7



8

9

10

11

12



13

14

15

16

17

In Main Function , traverse the given range judge every number use the above function:










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:



























