AgPro

导航

生成质数数组

#include <iostream>
using namespace std;

const int MAXP = 200;
bool pm[MAXP+1];

void primes()
{	
	memset(pm,1,MAXP);
	int i,j;
	for ( i=2; i<=MAXP; i++ )
	{
		if ( pm[i] )
		{
			j = 2*i;
			while ( j<=MAXP )
			{
				pm[j] = false;
				j += i;
			}
		}
	}

}

int main()
{
	primes();
	int i;
	for ( i=2; i<=MAXP; i++ )
	{
		if ( pm[i] )		
			cout << i << " ";
	}
	cout << endl;
}

posted on 2010-06-11 10:13  AgPro  阅读(272)  评论(0编辑  收藏  举报