SGU 113 – Prime check & generate

/* Speed up to check prime
 * For x < 32000, return directly
 * otherwise use generated prime(<32000) to test
 */
#include<cstdio>
#include<cstring>
 
bool is[32000]={0};
int  p[3700],pn=0;
 
bool isPrime(int x)
{
	if(x<32000) return (!is[x]);
	else for(int i=0;i<pn;i++) if(x%p[i]==0) return false;
	return true;	
}
 
int main()
{
	int n,m,i,j;
	bool flag;
 
	is[0]=is[1]=true;
	for(i=2;i<32000;i++)
		if(!is[i]) {
			p[pn++]=i;
			for(j=i+i;j<32000;j+=i) is[j]=true;
		}
 
	scanf("%d",&n);
	while(n--)
	{
		scanf("%d",&m);
		flag=false;
		for(int i=0;i<pn;i++)
			if(m%p[i]==0&&isPrime(m/p[i])) { flag=true; break; }
			else if(m<p[i]*p[i]) break;
		if(flag) puts("Yes");
		else puts("No");		
	}	
	return 0;
}

Leave a Reply

Your email address will not be published.