1. 程式人生 > >求10000以內的素數

求10000以內的素數

#include "iostream"
#include "cmath"
#include "memory.h"
using namespace std;

const int Max = 10000;
bool numbers[Max];

void searchprime(){
	memset(numbers, true, sizeof(numbers));
	for (int i = 2; i < sqrt(Max) + 1; i++){
		if (numbers[i]){
			int j = 2 * i;
			int h = 2;
			while(j <= Max){
				numbers[j] = false;
				j = (++h) * i;
			}
		}
	}
	for (int i = 2; i < Max; i++){
		if (numbers[i])
			cout << i << " ";
	}
	cout << endl;
}

int main(){
	searchprime();

	return 0;
}