Three methods for prime number table are summarized.
First:
void intline(){ memset(prime,true,sizeof(prime)); prime[1]=false; prime[0]=false; for(int i=2; i*i<MAXN; i++) { for(int j=2; j*i<MAXN; j++) prime[j*i]=false; }}
Second: similar to the first principle, it is also the method used for this question, but this is an array opened with an int, prime [I] = J indicates that the maximum prime factor of I is the prime number J, and 2 is the first prime number). Therefore, this method has many variants.
void intline(){ int num=0; memset(prime,0,sizeof(prime)); for(int i=2;i<MAXN;i++) { if(!prime[i]) { num++; for(int j=i;j<MAXN;j+=i) prime[j]=num; } }}
The third method is the most common method.
int isprime(int n){ for(int i=2; i*i<=n; i++) if(n%i==0) return 0; return 1;}
Fourth type: in fact, like the second type, it is the first type of deformation. This prime [I] = J indicates that the prime number of I is J.
void intline(){ memset(num,0,sizeof(num)); num[0]=num[1]=1; int cnt=0; for(int i=2; i*i<MAXN; i++) { if(!num[i]) { prime[cnt++]=i; for(int j=2; j*i<MAXN; j++) num[j*i]=1; } }}
Code for this question:
#include<cstdio>#include<cstring>#include<iostream>#define MAXN 1000001using namespace std;int prime[MAXN];void intline(){ int num=0; for(int i=2;i<MAXN;i++) { if(!prime[i]) { num++; for(int j=i;j<MAXN;j+=i) prime[j]=num; } }}int main(){ //freopen("in.txt","r",stdin); intline(); int n; while(scanf("%d",&n)!=EOF) { printf("%d\n",prime[n]); } return 0;}