View Code of Problem 103

#include<bits/stdc++.h>
#define maxn 1000001
using namespace std;
bool str[maxn];
int a[maxn];
void prime(){
	str[1]=1;
	for(int i=2;i*i<maxn;++i){
		if(!str[i]){
			for(int j=i*i;j<=maxn;j+=i)
			str[j]=1;
		}
	}
	for(int i=2;i<maxn;++i){
		if(!str[i])a[i]=a[i-1]+1;
		else
		a[i]=a[i-1];
	}
}
int main(){
	int n,m;
	memset(a,0,sizeof(a));
	prime();
	while(scanf("%d%d",&n,&m)!=EOF){
		printf("%d\n",a[m]-a[n-1]);
	}
}

Double click to view unformatted code.


Back to problem 103