NYOJ 187 快速查找素数

原题链接

打表。

附ac代码:

#include <stdio.h>
#include <math.h>
#define MAX 2000000 + 2
int a[MAX] = {1, 1};

void count(){
	int t = sqrt(MAX);
	for(int i = 2; i <= t; ++i){
		if(a[i]) continue;
		for(int j = i * i; j <= MAX; j += i)
			a[j] = 1;
	}
}

int main(){
	count();
	int n, i;
	while(scanf("%d", &n), n){
		for(i = 2; i <= n; ++i)
			if(!a[i]) printf("%d ", i);
		printf("\n");
	}
	return 0;
}


posted on 2014-02-14 17:54  长木Qiu  阅读(121)  评论(0编辑  收藏  举报