circular prime number program in Java

Circular Prime: A circular prime number is a prime number and also when circularly permuting its digits will be prime.
For instance 1931 is a circular prime since 1931 is prime and also its circular permuting digits i.e., 1193, 3119, 9311 are also prime numbers.
Let's take another small example that is 29, here in this even though 29 is prime number, its circular permuting number 92 is not a prime number hence we can conclude that 29 is not a Circular Prime number.

Let's jump on to writing program for finding whether a given number is circular prime number or not. The easy and very efficient way to find whether a given is Circular prime or not with following steps.
Step 1: Find the length of the given number, while finding length check whether any digit is even.
Step 2: If any digit is even, print that given number is not a Circular Prime Number and stop the execution. If not go to Step 3.
Step 3: First check whether the given number is Prime or not. If it is Prime then permute the number circularly. Else print that given number is not a Circular Prime Number and stop the execution.
Step 4: Now check  the current computed number is prime or not. If it Prime the continue to permute the new number and check whether it is prime number or not.

Note: As we are checking prime number or not, we should select an efficient algorithm for examine a number is prime number or not.

Program Implementation:

/**
 * This program is to find a given number is
 * Circular Prime number or not.
 * @author Technical Explorer
 */
package com.technicalexplorer;

import java.util.Scanner;

public class CircularPrimeNumber {
public boolean isPrimeEfficient(int number) {
 boolean isPrime = true;
 /**
  * return true if number is 2 as it is
  * the only even number which is also prime.
  */
 if(number == 2) {
  return isPrime;
 }

 //return false if number is less than 2 or even number
 if(number < 2 || number%2 == 0) {
  return !isPrime; //Returning false
 }

 /**
  * Checking for divisor till square root of
  * the given number + 1 in case number is
  * round off while finding square root.
  *
  */
 int sqrtOfNumber = (int)Math.sqrt(number);
 for(int i=3; i<=sqrtOfNumber; i=i+2) {
  if(number%i == 0) {
   //returning false as number is divisible by i
   return !isPrime;
  }
 }

 /**
  * returning true as number has no positive
  * divisors other than 1 and itself
  */
 return isPrime;

}

/**
* Splitting given number into array of digits
* @param number
* @return array of digits
*/
public int[] divideIntoDigits(int number) {
//Finding length of the given number and then create an array of same length
int length = String.valueOf(number).length();
int[] digits = new int[length];
int count=0;
while(number > 0) {
digits[count++] = number%10;
number = number/10;
}
return digits;
}

public boolean isCircularPrime(int number) {
boolean flag = true;
//Trivial case, here we are quick response
//for single digit numbers in order to avoid all computations
if(number == 2 || number == 3 || number == 5 || number == 7) {
return flag;
} else if(number < 10) {
return false;
}

//Divide given number into digits and store them in an array
int[] digits = divideIntoDigits(number);
int length = digits.length;
//Check any digit is an even number, if number is even
//then return false else continue further
for(int i=0; i<length; i++) {
if(digits[i] % 2 == 0) {
return false;
}
}

//Check first whether the given number is prime or not
if(!isPrimeEfficient(number)) {
return false;
}

for(int i=1; i<length; i++) {
number = (number - digits[length-i]*(int)Math.pow(10, length-1))*10 + digits[length-i];
flag = isPrimeEfficient(number);
if(!flag) {
return flag;
}
}
return flag;
}

public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter the number: ");
int number = scanner.nextInt();
scanner.close();
CircularPrimeNumber cpn = new CircularPrimeNumber();
if(cpn.isCircularPrime(number)) {
System.out.println(number + " is a Circular Prime Number!!!");
} else {
System.out.println(number + " is not a Circular Prime Number!!!");
}
}
}

Sample Input and Output
1. Input: Please enter number: 1931
    Output: 1931 is a Circular Prime Number

2. Input: Please enter number: 1933
    Output: 1933 is not a Circular Prime Number

If I missed anything please comment below.
All the very best!!

Powered by Blogger.