Spoj Solution PRIONPRI - Prime or not Prime!
Solve in C++:
#include<bits/stdc++.h>
using namespace std;
main()
{
long long int n,i,a,r,j,f=0;
scanf("%lld",&n);
for(i=1;i<=n;i++)
{
scanf("%lld",&a);
r=sqrt(a);
for(j=2;j<=r;j++)
{
if(a%j==0)
{
f=1;
break;
}
}
if(f==1)
printf("NO\n");
else
printf("YES\n");
f=0;
}
return 0;
}
#include<bits/stdc++.h>
using namespace std;
main()
{
long long int n,i,a,r,j,f=0;
scanf("%lld",&n);
for(i=1;i<=n;i++)
{
scanf("%lld",&a);
r=sqrt(a);
for(j=2;j<=r;j++)
{
if(a%j==0)
{
f=1;
break;
}
}
if(f==1)
printf("NO\n");
else
printf("YES\n");
f=0;
}
return 0;
}
This solution is too slow .... Did you even get AC for this on spoj?
ReplyDeleteBrother it's AC code
Delete