Skip to main content

Circular Prime Number

A circular prime is a prime number that remains prime under circular shifts of its digits. In other words, if you rotate its digits in any way, the resulting number is still a prime number. For example, the number 197 is a circular prime, because 197, 971, and 719 are all prime numbers.

There are several methods to determine whether a given number is a circular prime or not. One way is to generate all possible rotations of its digits and check if each resulting number is prime. Another way is to use number theory and the properties of prime numbers to determine whether a number is a circular prime.

Some examples of circular primes are:

  • 2
  • 3
  • 5
  • 7
  • 11
  • 13
  • 17
  • 31
  • 37
  • 71
  • 73
  • 79
  • 97
  • 113
  • 131
  • 199
  • 311
  • 337
  • 373
  • 733
  • 919
  • 991

Note that the number of circular primes is finite, but their distribution is not uniform. For example, there are only 4 circular primes with two digits (11, 13, 17, and 37), while there are 13 circular primes with three digits.

Test 1 : is 431 circular primes?

Yes, 431 is a circular prime.

To see why, we can generate all possible rotations of its digits:

  • 431
  • 314
  • 143

We can see that all three resulting numbers are prime, so 431 is a circular prime.

Example 2: is 1771 circular primes?

No, 1771 is not a circular prime. To determine whether a number is a circular prime or not, we need to check whether all its circular permutations (rotations) are also prime.

In the case of 1771, its circular permutations are 1771, 7711, 7117, and 1177. However, 7711 and 1177 are not prime numbers, since they are both divisible by 7. Therefore, 1771 is not a circular prime.

Note that the only digits that a circular prime can contain are 1, 3, 7, and 9, because if it contains any even digit or 5, at least one of its circular permutations would be divisible by 2 or 5, respectively, and therefore not prime.

Here given code implementation process.

//C Program
//Check if a given number is Circular Prime or not
#include <stdio.h>
#include <math.h>
//Check number is prime or not
int is_prime(int n)
{
  if(n<=1)
  {
    return 0;
  }
  //Base case
  if(n==2 || n ==3 || n==5)
  {
    return 1;
  }
  
  for (int i = n/2; i > 1 ; --i)
  {
    //Check divisibility of a number
    if(n%i == 0)
    {
      return 0;
    }
  }
  
  return 1;
}
//Count the number of digits in number 
int digit_length(int number) 
{ 
  int size = 0; 

  while (number!=0) 
  { 
    number/=10; 

    size++; 
  } 
  return size; 
}


int is_circular_prime(int number) 
{ 
  //Assign the value of how many number of digits are exist in number
  int  size = digit_length(number);

  int auxiliary = number;

  //Variable which control  execution process
  int remainder = 0;

  int divisor = 0;

  while (is_prime(auxiliary)==1) { 

    //Calculate divisor of auxiliary number
    divisor = auxiliary / 10;

    //Calculate remainder of auxiliary number
    remainder = auxiliary % 10; 
    
    //permutation calculation
    auxiliary = (pow(10, size - 1)) * remainder + divisor; 
    
    //When permutation of number is equal to actual number
    //T
    if (auxiliary == number) 
    {
      return 1; 
    }
  } 

  return 0; 
} 
//Function which are handling the request of number is circular prime or not
void circular_prime(int number)
{
  if(is_circular_prime(number)==1)
  {
    //When Yes
    printf("%d Is an Circular Prime Number\n",number);
  } 
  else
  {
    //When No
    printf("%d Is not a Circular Prime Number\n",number);
  }
}

int main() {
  //Test Cases
  circular_prime(71);
  circular_prime(9311);
  circular_prime(23);
  circular_prime(43);
  circular_prime(13);

  return 0;
}

Output

71 Is an Circular Prime Number
9311 Is an Circular Prime Number
23 Is not a Circular Prime Number
43 Is not a Circular Prime Number
13 Is an Circular Prime Number
/*
 C++ Program
 Check if a given number is Circular Prime or not
*/
#include<iostream>
#include <math.h>
using namespace std;

class MyNumber {
	public:

		//Check number is prime or not
		int is_prime(int n) {
			if (n <= 1) {
				return 0;
			}
			//Base case

			if (n == 2 || n == 3 || n == 5) {
				return 1;
			}
			for (int i = n / 2; i > 1; --i) {
				//Check divisibility of a number

				if (n % i == 0) {
					return 0;
				}
			}
			return 1;
		}
	//Count the number of digits in number 
	int digit_length(int number) {
		int size = 0;
		while (number != 0) {
			number /= 10;
			size++;
		}
		return size;
	}
	int is_circular_prime(int number) {
		//Assign the value of how many number of digits are exist in number
		int size = this->digit_length(number);
		int auxiliary = number;
		//Variable which control  execution process
		int remainder = 0;
		int divisor = 0;
		while (this->is_prime(auxiliary) == 1) {
			//Calculate divisor of auxiliary number
			divisor = auxiliary / 10;
			//Calculate remainder of auxiliary number
			remainder = auxiliary % 10;
			//permutation calculation
			auxiliary = ((int)pow(10, size - 1)) *remainder + divisor;
			//When permutation of number is equal to actual number

			if (auxiliary == number) {
				return 1;
			}
		}
		return 0;
	}
	//Function which are handling the request of number is circular prime or not
	void circular_prime(int number) {
		if (this->is_circular_prime(number) == 1) {
			//When Yes

			cout << number << " Is an Circular Prime Number\n";
		} else {
			//When No

			cout << number << " Is not a Circular Prime Number\n";
		}
	}
};
int main() {
	MyNumber obj;
	//Test Cases
	obj.circular_prime(71);
	obj.circular_prime(9311);
	obj.circular_prime(23);
	obj.circular_prime(43);
	obj.circular_prime(13);
	return 0;
}

Output

71 Is an Circular Prime Number
9311 Is an Circular Prime Number
23 Is not a Circular Prime Number
43 Is not a Circular Prime Number
13 Is an Circular Prime Number
/*
  Java Program
  Check if a given number is Circular Prime or not
*/

public class MyNumber {

  //Check number is prime or not
  public int is_prime(int n)
  {
    if(n<=1)
    {
      return 0;
    }
    //Base case
    if(n==2 || n ==3 || n==5)
    {
      return 1;
    }
    
    for (int i = n/2; i > 1 ; --i)
    {
      //Check divisibility of a number
      if(n%i == 0)
      {
        return 0;
      }
    }
    
    return 1;
  }
  //Count the number of digits in number 
  public int digit_length(int number) 
  { 
    int size = 0; 

    while (number!=0) 
    { 
      number/=10; 

      size++; 
    } 
    return size; 
  }


  public int is_circular_prime(int number) 
  { 
    //Assign the value of how many number of digits are exist in number
    int  size = digit_length(number);

    int auxiliary = number;

    //Variable which control  execution process
    int remainder = 0;

    int divisor = 0;

    while (is_prime(auxiliary)==1) { 

      //Calculate divisor of auxiliary number
      divisor = auxiliary / 10;

      //Calculate remainder of auxiliary number
      remainder = auxiliary % 10; 
      
      //permutation calculation
      auxiliary = ((int)Math.pow(10, size - 1)) * remainder + divisor; 
      
      //When permutation of number is equal to actual number
      if (auxiliary == number) 
      {
        return 1; 
      }
    } 

    return 0; 
  } 
  //Function which are handling the request of number is circular prime or not
  public void circular_prime(int number)
  {
    if(is_circular_prime(number)==1)
    {
      //When Yes
      System.out.print(number+" Is an Circular Prime Number\n");
    } 
    else
    {
      //When No
      System.out.print(number+" Is not a Circular Prime Number\n");
    }
  }

  public static void main(String[] args) {

    MyNumber obj = new MyNumber();
    //Test Cases
    obj.circular_prime(71);
    obj.circular_prime(9311);
    obj.circular_prime(23);
    obj.circular_prime(43);
    obj.circular_prime(13);

  }
}

Output

71 Is an Circular Prime Number
9311 Is an Circular Prime Number
23 Is not a Circular Prime Number
43 Is not a Circular Prime Number
13 Is an Circular Prime Number
/*
  C# Program
  Check if a given number is Circular Prime or not
*/
using System;
public class MyNumber {

	//Check number is prime or not
	public int is_prime(int n) {
		if (n <= 1) {
			return 0;
		}
		//Base case
		if (n == 2 || n == 3 || n == 5) {
			return 1;
		}

		for (int i = n / 2; i > 1; --i) {
			//Check divisibility of a number
			if (n % i == 0) {
				return 0;
			}
		}

		return 1;
	}
	//Count the number of digits in number 
	public int digit_ength(int number) {
		int size = 0;

		while (number != 0) {
			number /= 10;

			size++;
		}
		return size;
	}


	public int is_circular_prime(int number) {
		//Assign the value of how many number of digits are exist in number
		int size = digit_ength(number);

		int auxiliary = number;

		//Variable which control  execution process
		int reMainder = 0;

		int divisor = 0;

		while (is_prime(auxiliary) == 1) {

			//Calculate divisor of auxiliary number
			divisor = auxiliary / 10;

			//Calculate reMainder of auxiliary number
			reMainder = auxiliary % 10;

			//permutation calculation
			auxiliary = ((int) Math.Pow(10, size - 1)) * reMainder + divisor;

			//When permutation of number is equal to actual number
			if (auxiliary == number) {
				return 1;
			}
		}

		return 0;
	}
	//Function which are handling the request of number is circular prime or not
	public void circular_prime(int number) {
		if (is_circular_prime(number) == 1) {
			//When Yes
			Console.Write(number + " Is an Circular Prime Number\n");
		} else {
			//When No
			Console.Write(number + " Is not a Circular Prime Number\n");
		}
	}

	public static void Main(String[] args) {

		MyNumber obj = new MyNumber();
		//Test Cases
		obj.circular_prime(71);
		obj.circular_prime(9311);
		obj.circular_prime(23);
		obj.circular_prime(43);
		obj.circular_prime(13);

	}
}

Output

71 Is an Circular Prime Number
9311 Is an Circular Prime Number
23 Is not a Circular Prime Number
43 Is not a Circular Prime Number
13 Is an Circular Prime Number
# Python 3 Program
# Check if a given number is Circular Prime or not

class MyNumber :
	#Check number is prime or not
	def is_prime(self, n) :
		if (n <= 1) :
			return 0
		
		#Base case

		if (n == 2 or n == 3 or n == 5) :
			return 1
		
		i = int(n / 2)
		while (i > 1) :
			#Check divisibility of a number

			if (n % i == 0) :
				return 0
			
			i -= 1
		
		return 1
	
	#Count the number of digits in number 
	def digit_length(self, number) :
		size = 0
		while (number != 0) :
			number = int(number / 10)
			size += 1
		
		return size
	
	def is_circular_prime(self, number) :
		size = self.digit_length(number)
		auxiliary = number
		remainder = 0
		divisor = 0
		while (self.is_prime(auxiliary) == 1) :
			#Calculate divisor of auxiliary number
			divisor = int(auxiliary / 10)
			#Calculate remainder of auxiliary number
			remainder = auxiliary % 10
			#permutation calculation
			auxiliary = (10**(size - 1)) * remainder + divisor
			#When permutation of number is equal to actual number

			if (auxiliary == number) :
				return 1
			
		
		return 0
	
	#Function which are handling the request of number is circular prime or not
	def circular_prime(self, number) :
		if (self.is_circular_prime(number) == 1) :
			print(number ," Is an Circular Prime Number\n")
		else :
			print(number ," Is not a Circular Prime Number\n")
		
	

def main() :
	obj = MyNumber()
	obj.circular_prime(71)
	obj.circular_prime(9311)
	obj.circular_prime(23)
	obj.circular_prime(43)
	obj.circular_prime(13)


if __name__ == "__main__":
	main()

Output

71 Is an Circular Prime Number
9311 Is an Circular Prime Number
23 Is not a Circular Prime Number
43 Is not a Circular Prime Number
13 Is an Circular Prime Number
# Ruby Program 
# Check if a given number is Circular Prime or not

class MyNumber 
	#Check number is prime or not
	def is_prime(n) 
		if (n <= 1) 
			return 0
		end
		#Base case

		if (n == 2 or n == 3 or n == 5) 
			return 1
		end
		i = n / 2
		while (i > 1) 
			#Check divisibility of a number

			if (n % i == 0) 
				return 0
			end
			i -= 1
		end
		return 1
	end
	#Count the number of digits in number 
	def digit_length(number) 
		size = 0
		while (number != 0) 
			number /= 10
			size += 1
		end
		return size
	end
	def is_circular_prime(number) 
		size = self.digit_length(number)
		auxiliary = number
		remainder = 0
		divisor = 0
		while (self.is_prime(auxiliary) == 1) 
			#Calculate divisor of auxiliary number
			divisor = auxiliary / 10
			#Calculate remainder of auxiliary number
			remainder = auxiliary % 10
			#permutation calculation
			auxiliary = (10**(size - 1)) * remainder + divisor
			#When permutation of number is equal to actual number

			if (auxiliary == number) 
				return 1
			end
		end
		return 0
	end
	#Function which are handling the request of number is circular prime or not
	def circular_prime(number) 
		if (self.is_circular_prime(number) == 1) 
			print(number ," Is an Circular Prime Number\n")
		else 
			print(number ," Is not a Circular Prime Number\n")
		end
	end
end
def main() 
	obj = MyNumber.new()
	obj.circular_prime(71)
	obj.circular_prime(9311)
	obj.circular_prime(23)
	obj.circular_prime(43)
	obj.circular_prime(13)
end
main()

Output

71 Is an Circular Prime Number
9311 Is an Circular Prime Number
23 Is not a Circular Prime Number
43 Is not a Circular Prime Number
13 Is an Circular Prime Number
/*
 Scala Program
 Check if a given number is Circular Prime or not
*/
class MyNumber {
	//Check number is prime or not
	def is_prime(n: Int): Int = {
		if (n <= 1) {
			return 0;
		}
		//Base case

		if (n == 2 || n == 3 || n == 5) {
			return 1;
		}
		var i: Int = n / 2;
		while (i > 1) {
			//Check divisibility of a number

			if (n % i == 0) {
				return 0;
			}
			i -= 1;
		}
		return 1;
	}
	//Count the number of digits in number 
	def digit_length(value: Int): Int = {
		var size: Int = 0;
      	var number: Int = value;
		while (number != 0) {
			number /= 10;
			size += 1;
		}
		return size;
	}
	def is_circular_prime(number: Int): Int = {
		var size: Int = this.digit_length(number);
		var auxiliary: Int = number;
		var remainder: Int = 0;
		var divisor: Int = 0;
		while (this.is_prime(auxiliary) == 1) {
			//Calculate divisor of auxiliary number
			divisor = auxiliary / 10;
			//Calculate remainder of auxiliary number
			remainder = auxiliary % 10;
			//permutation calculation
			auxiliary = (scala.math.pow(10, size-1)).toInt * remainder + divisor;
			//When permutation of number is equal to actual number

			if (auxiliary == number) {
				return 1;
			}
		}
		return 0;
	}
	//Function which are handling the request of number is circular prime or not
	def circular_prime(number: Int): Unit = {
		if (this.is_circular_prime(number) == 1) {
			print(s"$number Is an Circular Prime Number\n");
		} else {
			print(s"$number Is not a Circular Prime Number\n");
		}
	}
}
object Main {
	def main(args: Array[String]): Unit = {
		var obj: MyNumber = new MyNumber();
        obj.circular_prime(71);
        obj.circular_prime(9311);
        obj.circular_prime(23);
        obj.circular_prime(43);
        obj.circular_prime(13);
	}
}

Output

71 Is an Circular Prime Number
9311 Is an Circular Prime Number
23 Is not a Circular Prime Number
43 Is not a Circular Prime Number
13 Is an Circular Prime Number
/*
  Swift 4 Program
  Check if a given number is Circular Prime or not
*/
import Foundation
class MyNumber {
	//Check number is prime or not
	func is_prime(_ n: Int) -> Int {
		if (n <= 1) {
			return 0;
		}
		//Base case

		if (n == 2 || n == 3 || n == 5) {
			return 1;
		}
		var i: Int = n / 2;
		while (i > 1) {
			//Check divisibility of a number

			if (n % i == 0) {
				return 0;
			}
			i -= 1;
		}
		return 1;
	}
	//Count the number of digits in number 
	func digit_length(_ value: Int) -> Int {
		var size: Int = 0;
      	var number: Int = value;
		while (number != 0) {
			number /= 10;
			size += 1;
		}
		return size;
	}
	func is_circular_prime(_ number: Int) -> Int {
		let size: Int = self.digit_length(number);
		var auxiliary: Int = number;
		var remainder: Int = 0;
		var divisor: Int = 0;
		while (self.is_prime(auxiliary) == 1) {
			//Calculate divisor of auxiliary number
			divisor = auxiliary / 10;
			//Calculate remainder of auxiliary number
			remainder = auxiliary % 10;
			//permutation calculation
			auxiliary =  Int(pow(Double(10), Double(size-1))) * remainder + divisor;
			//When permutation of number is equal to actual number

			if (auxiliary == number) {
				return 1;
			}
		}
		return 0;
	}
	//Function which are handling the request of number is circular prime or not
	func circular_prime(_ number: Int) {
		if (self.is_circular_prime(number) == 1) {
			print(number ," Is an Circular Prime Number");
		} else {
			print(number ," Is not a Circular Prime Number");
		}
	}
}
func main() {
	let obj: MyNumber = MyNumber();
	obj.circular_prime(71);
	obj.circular_prime(9311);
	obj.circular_prime(23);
	obj.circular_prime(43);
	obj.circular_prime(13);
}
main();

Output

71  Is an Circular Prime Number
9311  Is an Circular Prime Number
23  Is not a Circular Prime Number
43  Is not a Circular Prime Number
13  Is an Circular Prime Number
<?php
/*
  Php Program
  Check if a given number is Circular Prime or not
*/
class MyNumber {
	//Check number is prime or not

	public 	function is_prime($n) {
		if ($n <= 1) {
			return 0;
		}
		//Base case

		if ($n == 2 || $n == 3 || $n == 5) {
			return 1;
		}
		for ($i = intval($n / 2); $i > 1; --$i) {
			//Check divisibility of a number

			if ($n % $i == 0) {
				return 0;
			}
		}
		return 1;
	}
	//Count the number of digits in number 

	public 	function digit_length($number) {
		$size = 0;
		while ($number != 0) {
			$number=intval($number / 10);
			$size++;
		}
		return $size;
	}
	public 	function is_circular_prime($number) {
		//Assign the value of how many number of digits are exist in number
		$size = $this->digit_length($number);
		$auxiliary = $number;
		//Variable which control  execution process
		$remainder = 0;
		$divisor = 0;
		while ($this->is_prime($auxiliary) == 1) {
			//Calculate divisor of auxiliary number
			$divisor = intval($auxiliary / 10);
			//Calculate remainder of auxiliary number
			$remainder = $auxiliary % 10;
			//permutation calculation
			$auxiliary = (pow(10, $size - 1)) *$remainder + $divisor;
			//When permutation of number is equal to actual number

			if ($auxiliary == $number) {
				return 1;
			}
		}
		return 0;
	}
	//Function which are handling the request of number is circular prime or not

	public 	function circular_prime($number) {
		if ($this->is_circular_prime($number) == 1) {
			//When Yes

			echo($number ." Is an Circular Prime Number\n");
		} else {
			//When No

			echo($number ." Is not a Circular Prime Number\n");
		}
	}
};

function main() {
	$obj = new MyNumber();
	//Test Cases

	$obj->circular_prime(71);
	$obj->circular_prime(9311);
	$obj->circular_prime(23);
	$obj->circular_prime(43);
	$obj->circular_prime(13);
}
main();

Output

71 Is an Circular Prime Number
9311 Is an Circular Prime Number
23 Is not a Circular Prime Number
43 Is not a Circular Prime Number
13 Is an Circular Prime Number
/*
 Node Js Program
 Check if a given number is Circular Prime or not
*/
class MyNumber {
	//Check number is prime or not
	is_prime(n) {
		if (n <= 1) {
			return 0;
		}
		//Base case

		if (n == 2 || n == 3 || n == 5) {
			return 1;
		}
		for (var i = parseInt(n / 2); i > 1; --i) {
			//Check divisibility of a number

			if (n % i == 0) {
				return 0;
			}
		}
		return 1;
	}
	//Count the number of digits in number 
	digit_length(number) {
		var size = 0;
		while (number != 0) {
			number = parseInt(number / 10);
			size++;
		}
		return size;
	}
	is_circular_prime(number) {
		//Assign the value of how many number of digits are exist in number
		var size = this.digit_length(number);
		var auxiliary = number;
		//Variable which control  execution process
		var remainder = 0;
		var divisor = 0;
		while (this.is_prime(auxiliary) == 1) {
			//Calculate divisor of auxiliary number
			divisor = parseInt(auxiliary / 10);
			//Calculate remainder of auxiliary number
			remainder = auxiliary % 10;
			//permutation calculation
			auxiliary = (parseInt(Math.pow(10, size - 1))) *remainder + divisor;
			//When permutation of number is equal to actual number

			if (auxiliary == number) {
				return 1;
			}
		}
		return 0;
	}
	//Function which are handling the request of number is circular prime or not
	circular_prime(number) {
		if (this.is_circular_prime(number) == 1) {
			//When Yes

			process.stdout.write(number + " Is an Circular Prime Number\n");
		} else {
			//When No

			process.stdout.write(number + " Is not a Circular Prime Number\n");
		}
	}
}

function main(args) {
	var obj = new MyNumber();
	//Test Cases
	obj.circular_prime(71);
	obj.circular_prime(9311);
	obj.circular_prime(23);
	obj.circular_prime(43);
	obj.circular_prime(13)
}
main();

Output

71 Is an Circular Prime Number
9311 Is an Circular Prime Number
23 Is not a Circular Prime Number
43 Is not a Circular Prime Number
13 Is an Circular Prime Number




Comment

Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.

New Comment