Lucas-Lehmer judge it.
#include <iostream>
#include <stdio.h>
#include <string.h>
#define LL long long
using namespace std;
LL p,sum,data[70],t;
LL multi(LL a,LL b,LL m){
LL ret=0;
while(b>0){
if(b&1) ret=(ret+a)%m;
b=b>>1;
a=(a<<1)%m;
}
return ret;
}
int main()
{
int T;
data[1]=4;
scanf("%d", &T);
while(T--){
sum=1;
scanf("%lld" ,&p);
sum=(sum<<p)-1;
for(int i=2;i<=p-1;++i){
t=multi(data[i-1] , data[i-1] , sum);
data[i]=(t-2)%sum;
}
if(p==2) printf("yes\n");
else {
if(data[p-1]==0) printf("yes\n");
else printf("no\n");
}
}
return 0;
}
miller_rabin
#include <stdio.h>
#include <math.h>
#include <cstring>
#include <algorithm>
#include <queue>
#include <vector>
#include <time.h>
#define LL long long
using namespace std;
const int S=80;
int t,p;
LL mod_mul(LL a,LL b,LL m){
LL ret=0;
while(b>0){
if(b&1) ret=(ret+a)%m;
b>>=1;
a=(a<<1)%m;
}
return ret;
}
LL mod_exp(LL a,LL b,LL m){
LL ans=1;
a%=m;
while(b){
if(b&1){
ans=mod_mul(ans,a,m);
b--;
}
b/=2;
a=mod_mul(a,a,m);
}
return ans;
}
bool miller_rabin(LL n){
if(n==2||n==3||n==5||n==7||n==11) return true;
if(n==1||!(n%2)||!(n%3)||!(n%5)||!(n%7)||!(n%11)) return false;
LL x,pre,u;
int i,j,k=0;
u=n-1;
while(!(u&1)){
k++; u>>=1;
}
srand((LL)time(0));
for(i=0;i<S;++i){
x=rand()%(n-2)+2;
if((x%n)==0) continue;
x=mod_exp(x,u,n);
pre=x;
for(j=0;j<k;++j){
x=mod_mul(x,x,n);
if(x==1&&pre!=1&&pre!=n-1) return false;
pre=x;
}
if(x!=1) return false;
}
return true;
}
int main(){
scanf("%d",&t);
while(t--){
scanf("%d",&p);
LL n=((LL)1<<p)-1;
if(miller_rabin(n)) printf("yes\n");
else printf("no\n");
}
return 0;
}