Circular Prime Number
PROBLEM :
A prime number is a Circular Prime Number if all of its possible rotations are itself prime numbers. Now given a number N check if it is Circular Prime or Not.
Input:
The first line of input contains an integer T denoting the number of test cases. Then T test cases follow. Each test case contains an integer N.
Output:
For each test case in a new line print 1 if the number is circular prime else 0.
Constraints:
1<=T<=100
1<=N<=104
Example:
Input:
2
197
101
Output:
1
0
Explanation:
For first test case:
197 is a Circular Prime because all rotations of 197 are
197, 719, 971 all of the 3 are prime number's
hence 197 is a circular prime
For second test case:
101 isn't a prime number as
rotation of 101 yields 110 which isn't a prime number
hence 101 isn't circular prime
--------------------------------------------------------------------------------
SIMPLE c++ IMPLEMENTATION :
--------------------------------------------------------------------------------
#include<iostream>
using namespace std;
#include<math.h>
#define max 100000
int Circular_Prime_Number(int ,int [],int) ;
void Sieve_of_Eratosthenes(int [],int ) ;
int No_digits(int ) ;
int rotate_no(int ,int ) ;
int main()
{
int arr[max] ;
Sieve_of_Eratosthenes(arr,max) ;
int t,no ;
cin>>t ;
while(t--)
{
cin>>no ;
cout<<Circular_Prime_Number(no,arr,max)<<endl ;
}
return 0;
}
int Circular_Prime_Number(int no,int arr[],int k)
{
if(no==0||no==1)
return 0 ;
int i,j,count,N ;
count=No_digits(no) ;
N=count ;
while(count--)
{
if(arr[no]==0)
return 0 ;
no=rotate_no(no,N) ;
if(no==0)
return 0 ;
}
return 1 ;
}
void Sieve_of_Eratosthenes(int arr[],int no)
{
int i,j;
for(i=0;i<=no;i++)
arr[i]=1 ;
arr[0]=0 ;
arr[1]=0 ;
for(i=2;i<=sqrt(no);i++)
{
if(arr[i]==1)
for(j=i+i;j<=no;j+=i)
arr[j]=0 ;
}
}
int No_digits(int no)
{
int i,count ;
count=0 ;
while(no)
{
count++ ;
no=no/10 ;
}
return count ;
}
int rotate_no(int no,int N)
{
int r ;
r=no%10 ;
if(r==0)
return 0 ;
no=no/10 ;
no=pow(10,N-1)*r+no ;
return no ;
}
---------------------------------------------------------------------------------
A prime number is a Circular Prime Number if all of its possible rotations are itself prime numbers. Now given a number N check if it is Circular Prime or Not.
Input:
The first line of input contains an integer T denoting the number of test cases. Then T test cases follow. Each test case contains an integer N.
Output:
For each test case in a new line print 1 if the number is circular prime else 0.
Constraints:
1<=T<=100
1<=N<=104
Example:
Input:
2
197
101
Output:
1
0
Explanation:
For first test case:
197 is a Circular Prime because all rotations of 197 are
197, 719, 971 all of the 3 are prime number's
hence 197 is a circular prime
For second test case:
101 isn't a prime number as
rotation of 101 yields 110 which isn't a prime number
hence 101 isn't circular prime
--------------------------------------------------------------------------------
SIMPLE c++ IMPLEMENTATION :
--------------------------------------------------------------------------------
#include<iostream>
using namespace std;
#include<math.h>
#define max 100000
int Circular_Prime_Number(int ,int [],int) ;
void Sieve_of_Eratosthenes(int [],int ) ;
int No_digits(int ) ;
int rotate_no(int ,int ) ;
int main()
{
int arr[max] ;
Sieve_of_Eratosthenes(arr,max) ;
int t,no ;
cin>>t ;
while(t--)
{
cin>>no ;
cout<<Circular_Prime_Number(no,arr,max)<<endl ;
}
return 0;
}
int Circular_Prime_Number(int no,int arr[],int k)
{
if(no==0||no==1)
return 0 ;
int i,j,count,N ;
count=No_digits(no) ;
N=count ;
while(count--)
{
if(arr[no]==0)
return 0 ;
no=rotate_no(no,N) ;
if(no==0)
return 0 ;
}
return 1 ;
}
void Sieve_of_Eratosthenes(int arr[],int no)
{
int i,j;
for(i=0;i<=no;i++)
arr[i]=1 ;
arr[0]=0 ;
arr[1]=0 ;
for(i=2;i<=sqrt(no);i++)
{
if(arr[i]==1)
for(j=i+i;j<=no;j+=i)
arr[j]=0 ;
}
}
int No_digits(int no)
{
int i,count ;
count=0 ;
while(no)
{
count++ ;
no=no/10 ;
}
return count ;
}
int rotate_no(int no,int N)
{
int r ;
r=no%10 ;
if(r==0)
return 0 ;
no=no/10 ;
no=pow(10,N-1)*r+no ;
return no ;
}
---------------------------------------------------------------------------------
Comments
Post a Comment