Eratosthenes筛计算N以内的素数

#include <iostream>
#include <vector>
#include <string>



using namespace std;

vector<int> GetPrimeNumber(const int N)
{
	vector<int> b;
	int* a = new int[N+1];
	a[1] = false;
	for (int i = 2; i < N + 1; i++)
	{
		a[i] = true;
	}
	int p = 2;
	int j = p*p;
	while (j <= N)
	{
		while (j<=N)
		{
			a[j] = false;
			j += p;
		}
		p++;
		while (a[p]==false)
		{
			p++;
		}
		j = p*p;
	}
	for (int i = 2; i < N + 1; i++)
	{
		if (a[i]==true)
		{
			b.push_back(i);
		}
	}
	return b;
	
}


int main()
{
	int N = 100;
	vector<int> b= GetPrimeNumber(N);
	
}

posted @ 2016-03-13 13:47  牧羊少年10号  阅读(85)  评论(0编辑  收藏  举报