下面是求2到10000之间的素数:
#include <list>
#include <iostream>
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
int index=1;
list<int> intlist;
list<int>::iterator it;
intlist.push_back(2);
cout<<"The prime number[ "<<index<<"] :"<<2<<endl;
for(int i=2;i<10000;i++){
bool mark=true;
for(it=intlist.begin();it!=intlist.end();it++)
{
if(i % (*it) == 0)
{
mark=false;
break;
}
}
if(mark)
{
index++;
intlist.push_back(i);
cout<<"The prime number[ "<<index<<"] :"<<i<<endl;
}
}
return 0;
}