#include<cstdio>
#include<cstring>
#include<algorithm>
#include<vector>
#include<cmath>
#include<cstdlib>
#define N 10000001
using namespace std;
vector<long int>prime;
bool arr[N];
void sieve()
{
long int k=sqrt(N);
for(int i=3; i<=k; i+=2)
{
if(arr[i]==0)
{
for(long int j=i*i; j<N; j+=2*i)
{
arr[j]=1;
}
}
}
arr[1]=1;
for(long int i=4; i<N; i+=2)
{
arr[i]=1;
}
prime.push_back(2);
for(long int i=3; i<N; i+=2)
{
if(arr[i]==0)
{
prime.push_back(i);
}
}
}
int main()
{
sieve();
char str[10], *pEnd;
long int num,limit,cnt,num1,loop,brk;
while(gets(str))
{
long int len=strlen(str);
if(str[0]=='0')
break;
if(len>=4)// 991 is the last Anagrammatic prime
{
printf("0\n");
continue;
}
num=strtol(str,&pEnd,10);
limit=1;
for(long int j=1; j<=len; j++)
limit*=10;
//printf(" limit %ld\n",limit);
brk=0;
for(long int i=num+1; i<limit; i++)
{
if(arr[i]==0)
{
sprintf(str,"%ld",i);
cnt=loop=0;
sort(str,str+len);
do
{
num1=strtol(str,&pEnd,10);
if(arr[num1]==0)
cnt++;
loop++;
}
while(next_permutation(str,str+len));
if(cnt==loop)
{
brk++;
printf("%ld\n",i);
break;
}
}
}
if(brk==0)
printf("0\n");
memset(str,'\0',sizeof(str));
}
return 0;
}
#include<cstring>
#include<algorithm>
#include<vector>
#include<cmath>
#include<cstdlib>
#define N 10000001
using namespace std;
vector<long int>prime;
bool arr[N];
void sieve()
{
long int k=sqrt(N);
for(int i=3; i<=k; i+=2)
{
if(arr[i]==0)
{
for(long int j=i*i; j<N; j+=2*i)
{
arr[j]=1;
}
}
}
arr[1]=1;
for(long int i=4; i<N; i+=2)
{
arr[i]=1;
}
prime.push_back(2);
for(long int i=3; i<N; i+=2)
{
if(arr[i]==0)
{
prime.push_back(i);
}
}
}
int main()
{
sieve();
char str[10], *pEnd;
long int num,limit,cnt,num1,loop,brk;
while(gets(str))
{
long int len=strlen(str);
if(str[0]=='0')
break;
if(len>=4)// 991 is the last Anagrammatic prime
{
printf("0\n");
continue;
}
num=strtol(str,&pEnd,10);
limit=1;
for(long int j=1; j<=len; j++)
limit*=10;
//printf(" limit %ld\n",limit);
brk=0;
for(long int i=num+1; i<limit; i++)
{
if(arr[i]==0)
{
sprintf(str,"%ld",i);
cnt=loop=0;
sort(str,str+len);
do
{
num1=strtol(str,&pEnd,10);
if(arr[num1]==0)
cnt++;
loop++;
}
while(next_permutation(str,str+len));
if(cnt==loop)
{
brk++;
printf("%ld\n",i);
break;
}
}
}
if(brk==0)
printf("0\n");
memset(str,'\0',sizeof(str));
}
return 0;
}
কোন মন্তব্য নেই:
একটি মন্তব্য পোস্ট করুন