Circular Prime Number | Practice | GeeksforGeeks

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.
 
Example 1:
Input: N = 197
Output: 1
Explanation: 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.

Example 2:
Input: N = 101
Output: 0
Explanation: 101 and 11 is prime but 110 is
not a prime number.

 
Your Task:
You don't need to read or print anything. Your task is to complete the function isCircularPrime() which takes N as input parameter and returns 1 if it is Circular Prime otherwise returns 0.
 
Expected Time Complexity: O(Nlog(log(N))
Expected Space Complexity: O(N)
 
Constraints:
1 <= N <= 105
0
raunakgiri212 days ago
C++ time=0.01s
 
public:int highPow(int n){    int count=0;    while(n)    {        count++;        n=n/10;    }    return count;}bool isPrime(int n){    if(n<=1)return 0;    for(int i=2;i<=sqrt(n);i++)    {        if(n%i==0)return 0;    }    return 1;}int isCircularPrime(int n) {    // Code here    if(!isPrime(n))return 0;    if(!n/10)return 1;    int p=highPow(n);    int m=n;    for(int i=0;i<p;i++)    {        m=m/10+(m%10)*pow(10,p-1);        if(!isPrime(m))return 0;    }    return 1;}
+1
ozodtagoev2 months ago
C++ solution. Time: O(sqrt(N)*log(N)),  Space: O(1)
bool isPrime(int n) {
    if(n <= 1) return false;
    for(int i = 2; i*i <= n; i++) if(n%i == 0) return false;
    return true;
}
int isCircularPrime(int n) {
    // Code here
    if(n <= 1) return 0;
    int digitCount = ceil(log10(n));
    int p = digitCount-1;
    while(digitCount--) {
        if(!isPrime(n)) return 0;
        n = (n%10*pow(10,p)) + n/10;
    }
    return 1;
}

-1
ncln538jsgdizpte5utrdkgdmv73rde23vovyyh52 months ago
     v.push_back(n); 
0
immodi_303 months ago
c++ soln
bool isPrime(int n)
    {
    // Corner cases
    if (n <= 1)
        return false;
    if (n <= 3)
        return true;
  
    // This is checked so that we can skip
    // middle five numbers in below loop
    if (n % 2 == 0 || n % 3 == 0)
        return false;
  
    for (int i = 5; i * i <= n; i = i + 6)
        if (n % i == 0 || n % (i + 2) == 0)
            return false;
  
    return true;
    }
  
	int isCircularPrime(int n) 
	{
	    string s = to_string(n);
	    int nn=s.length();
	    
	    if(isPrime(n)==false)
	    return 0;
	    
	    if(s.size() ==1)
	    return 1;
	    
	    for(int i=0;i<nn-1;i++)
	    {
	        string lst;
	        lst = s[s.length()-1];
	        s=s.substr(0,s.length()-1);
	        //cout<<lst <<"  "<<s<<" "<<endl;
	        
	        s= lst+s; //this appoints s as 7 19 then 9 71 then main last 1 97 but we dont need to check that
	        
	        stringstream geek(s);
	        int x;
	        geek>>x;
	        if(isPrime(x)==false)
	        return 0;
	    }
	    return 1;
	}
  
0
aloksinghbais025 months ago
public:bool isPrime[100001];Solution(){    memset(isPrime,true,sizeof(isPrime));}void precomputation(){    for(int num=2; num<=100000; num++){        long long int j = 1LL*num*num;        if(j>100000) return;        for(int d=num*num; d<=100000; d+=num){            isPrime[d] = false;        }    }}int convertToInt(vector<int> v){    int num = 0;    int base = 1;    for(int i=v.size()-1; i>=0; i--){        num += v[i]*base;        base *= 10;    }    return (num);}bool checkAllRotations(int num){    if(!isPrime[num]) return (false);    int temp = num;    vector<int> dnum;    while(temp > 0){        dnum.push_back(temp%10);        temp /= 10;    }        reverse(dnum.begin(),dnum.end());    int sz = dnum.size();        for(int cnt=1; cnt<sz; cnt++){        int last = dnum[sz-1];        for(int i=sz-1; i>0; i--) dnum[i] = dnum[i-1];        dnum[0] = last;                int temp = convertToInt(dnum);        if(!isPrime[temp]) return (false);    }    return (true);}int isCircularPrime(int n) {    if(n==1) return(false);    precomputation();    return (checkAllRotations(n) ? 1 : 0);}
+1
aloksinghbais02
This comment was deleted.
0
anutiger6 months ago
	    vector<int> dp(1e5 + 1,1);
	    for(int i = 2; i <= 1e5; i++){
	        if(dp[i] == 1){
	            int j = 2;
	            while(i * j <= 1e5){
	                dp[i * j] = 0;
	                j++;
	            }
	        }
	    }
	    int size = 0;
	    int s = to_string(n).length();
	    if(dp[n] == 0 or n == 1)
	        return 0;
	    while(size < s){
	        int a = n % 10;
	        n/= 10;
	        n += (a * pow(10, s - 1));
	        size++;
	        if(dp[n] == 0)
	            return 0;
	    }
	    return 1;
0
humblefool2point0
This comment was deleted.
-1
siddharthdhaka146 months ago
class Solution
{
   public int isCircularPrime(int n)
   {
       if(n == 1) return 0;
       if(n == 2) return 1;
       
       int nn = n;
       
       while(true){
           if(!primeChecker(nn)) return 0;
           
           nn = (int)nn%10 *   (int)java.lang.Math.pow(10,(int)java.lang.Math.log10(nn)) + (int)nn/10;

 
           if(nn == n) break;
       }
       return 1;   
   }
   
   
  public boolean primeChecker(int n){
       int sqroot1 = (int)java.lang.Math.sqrt(n) + 1;

       for (int i = 2; i <= sqroot1; i++){
           if(n%i == 0)  return false;            
       }   
       return true;
   }
}
0
harsheopro6 months ago
public:bool isprime(int n){    for(int i=2;i<n;i++){        if(n%i==0)        return false;    }    return true;}int isCircularPrime(int n) {    if(n==1)    return 0;    int d=n;    int count =0;    int p=1;    while(d!=0){        int r=d%10;        count++;        p*=10;        d/=10;    }    for(int i=0;i<count;i++){        if(isprime(n)){                    }        else        return 0;        n=(n%10)*(p/10)+(n/10);    }    return 1;}};
We strongly recommend solving this problem on your own before viewing its editorial. Do you still
                    want to view the editorial?
 Login to access your submissions. 

Problem


Contest

Reset the IDE using the second button on the top right corner.
Avoid using static/global variables in your code as your code is tested against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases does not guarantee the correctness of code. On submission, your code is tested against multiple test cases consisting of all possible corner cases and stress constraints.
You can access the hints to get an idea about what is expected of you as well as the final solution code.
You can view the solutions submitted by other users from the submission tab.