w3resource

C++ Exercises: Find circular prime numbers upto a specific limit

C++ Numbers: Exercise-31 with Solution

Write a program in C++ to find circular prime numbers upto a specific limit.

Sample Solution:

C++ Code :

#include<iostream>
#include<cmath>

using namespace std;

int flg;
void chkPrime(long int n)
{
    long int i;
    i=n-1;
    while(i>=2)
    {
        if(n%i==0)
        {
            flg=1;
        }
        i--;
    }
}

void AllCombination(long int a)
{
  long int b,c,d,e,i,j,k,s,z,v,x[8],y[8],m;
  b=a;
  i=0;
  while(b>0)
  {
      y[i]=b % 10;
      b=b/10;
      i++;
  }
  c=0;
  for(j=i-1;j>=0;j--)
  {
    x[c]=y[j];
    c++;
  }
  m=i;
  while(m>0)
  {
     c=m-1;
     d=i-1;
     e=0;
     s=0;
     while(e<i)
     {
       z=pow(10,d);
       v=z*x[c%i];
       c++;
       d--;
       e++;
       s=s+v;
     }
     m--;
     chkPrime(s);
  }
}

int main()
{
    long int i=2,ctr;
	
 cout << "\n\n Find Circular Prime Numbers upto a specific limit: \n";
 cout << " ---------------------------------------------------\n";
    cout<<" Enter the upper Limit: ";
    cin>>ctr;
    cout<<"\n The Circular Prime Numbers less than "<<ctr<<" are: "<<endl;
    while(i<=ctr)
    {
      flg=0;
      AllCombination(i);
      if(flg==0)
      {
          cout<<i<<" ";
      }
      i++;
    }
	cout<<endl;

}

OR

C++ Code:


#include <bits/stdc++.h>
using namespace std;
 
void SieveOfSundaram (bool marked[], int);
int Rotate(int );
int countDigits(int );
 
void circularPrime(int n)
{
    int nNew = (n-2)/2;
    bool marked[nNew + 1];
    memset(marked, false, sizeof(marked));
    SieveOfSundaram(marked, nNew);
    cout << "2 " ;
    for (int i=1; i<=nNew; i++)
    {
        if (marked[i] == true)
            continue;
        int num = 2 * i + 1;
        num = Rotate(num); // function for rotation of prime
        while (num != 2*i + 1)
        {
            if (num % 2 == 0) // check for even
                break;
            if (marked[(num - 1)/2] == false)
                num = Rotate(num);
            else
                break;
        }
        if (num == (2*i + 1))
            cout << num << " ";
    }
}
 
void SieveOfSundaram(bool marked[], int nNew)
{
    for (int i=1; i<=nNew; i++)
        for (int j=i; (i + j + 2*i*j) <= nNew; j++)
            marked[i + j + 2*i*j] = true;
}
int Rotate(int n)
{
    int rem = n % 10;          // find unit place number
    rem *= pow(10, countDigits(n)); // to put unit place 
    n /= 10;                   // remove unit digit
    n += rem;                  // add first digit to rest 
    return n;
}
 
int countDigits(int n)
{
    int digit = 0;
    while (n /= 10)
        digit++;
    return digit;
}
 
int main(void)
{
    int n = 100;
    circularPrime(n);
    return 0;
}

Sample Output:

Find Circular Prime Numbers upto a specific limit:                    
 ---------------------------------------------------                   
 Enter the upper Limit: 248                                            
                                                                       
 The Circular Prime Numbers less than 248 are:                         
2 3 5 7 11 13 17 31 37 71 73 79 97 113 131 197 199  

Flowchart:

Flowchart: Find circular prime numbers upto a specific limit
Flowchart: Find circular prime numbers upto a specific limit
Flowchart: Find circular prime numbers upto a specific limit

C++ Code Editor:

Contribute your code and comments through Disqus.

Previous: Write a program in C++ to check if a given number is circular prime or not.
Next: Write a program in C++ to check whether a given number is a perfect cube or not.

What is the difficulty level of this exercise?



Share this Tutorial / Exercise on : Facebook and Twitter

C++ Programming: Tips of the Day

How to use the PI constant in C++?

On some (especially older) platforms (see the comments below) you might need to

#define _USE_MATH_DEFINES and then include the necessary header file:
#include<math.h>

and the value of pi can be accessed via:

M_PI

In math.h (2014) it is defined as:

# define M_PI           3.14159265358979323846  /* pi */

but check your math.h for more. An extract from the "old" math.h (in 2009):

/* Define _USE_MATH_DEFINES before including math.h to expose these macro
 * definitions for common math constants.  These are placed under an #ifdef 
 * since these commonly-defined names are not part of the C/C++ standards.
 */

However:

  1. on newer platforms (at least on my 64 bit Ubuntu 14.04) I do not need to define the _USE_MATH_DEFINES
  2. On (recent) Linux platforms there are long double values too provided as a GNU Extension:
# define M_PIl          3.14159265358979323846

Ref: https://bit.ly/3G4BgzQ