wap to input no and check it is circular prime no or not
Answers
Circular Prime - Java
A Circular Prime is a prime number which remains a prime even when its digits are permuted in a circular manner.
For example, 197 is a Circular Prime. Because:
- 197 is a Prime
- 719 is also a Prime
- 971 is yet again a Prime
Since all circular permutations are primes, 197 is a Circular Prime.
For a larger example, consider 39119
- 39119 is a Prime
- 93911 is also a Prime
- 19391 is also a Prime
- 11939 is also a Prime
- 91193 is also a Prime
All circular permutations are primes. So, 39119 is a Circular Prime.
Logic of Program
One thing is clear that a circular prime cannot contain the digits 2, 4, 5, 6, 8 or 0. Because the presence of any of these digits in the number will result in a Circular Permutation where they are in the Unit's Place, and then that Permutation Number is a Composite.
So, we can do an initial validity check where we declare a number Not Circular Prime if it contains any of the digits.
A Circular Prime can only contain the digits 1, 3, 7 and 9.
We will run a loop to get the number of digits. After this, we can run a loop again to circularly permute the number. For this, we can remove the unit's digit and place it at the leftmost position. This can be done by a few mathematical operations as seen in the Program.
For each circular permutation we also check if it is a Prime or not. If not, we terminate program and declare Not Circular Prime.
If all validations are passed and all circular permutations turn out to be Prime, we can declare a number to be Circular Prime.
//Circular Primes
import java.util.Scanner;
public class CircularPrime
{
//Function to check if a number is Prime
static boolean isPrime(int num)
{
//We check divisibility by numbers from 2 to sqrt(num)
//If divisible, we return false
//If no divisibility found, number is Prime
for(int i=2;i<=(int)Math.sqrt(num);i++)
{
if(num%i==0)
{
return false;
}
}
return true;
}
/* Circular Primes Logic
*
* Circular Primes involve Circular Permutations of a number
* If a number has any of the digits 0, 2, 4, 5, 6 or 8, it cannot be a Circular Prime
* These digits immediately rule out possibility of Circular Primes
* So, a Circular Prime can only contain the digits 1, 3, 7 and 9.
*/
//Function to check if a number is Circular Prime
static boolean isCircularPrime(int num)
{
int numberOfDigits = 0; //Variable to store number of digits in num
int num2 = num; //Temporary variable to get number of digits
//While extracting number of digits from num2,
// we will also check if the digits are 0, 2, 4, 5, 6 or 8
// If these digits are present, we return false
while(num2!=0)
{
int digit = num2%10; //Extracting unit's digit
if(digit%2==0 || digit%5 == 0) //Validating digit
{
return false;
}
numberOfDigits++;
num2 /= 10; //Dividing num by 10 for next loop iteration
}
//The given number must be a Prime
//If not, return false
if(!isPrime(num))
{
return false;
}
//Now we begin a loop to circularly permute the number num
for(int i=1;i<numberOfDigits;i++)
{
int unitsDigit = num%10; //Extracting Units Digit
num /= 10; //Dividing num by 10
//Now we do num + unitsDigit*10^(numberOfDigits-1)
//This gives us num with Units Digit at the front
num += unitsDigit*(int)Math.pow(10,numberOfDigits-1);
//Check this new number if it is Prime
//If not, return false
if(!isPrime(num))
{
return false;
}
}
//Even after all of this, if the function manages to reach this point
// then the number is Circular Prime
//Return true
return true;
}
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
System.out.print("Enter a number: ");
int num = sc.nextInt(); //Taking User Input
if(isCircularPrime(num)) //Invoking Function
{
System.out.println(num+" is a Circular Prime.");
}
else
{
System.out.println(num+" is Not a Circular Prime.");
}
}
}
Answer:
import java.util.*;
public class CircularPrime
{
int n, m, c, f, i, a, b, j, f1; // Instance Variables
void circularPrime(int x)
{
c=0; // Total Digit Calculate
n=x;
f=0;
while(n!=0)
{
n/=10;
c++;
}
n=x;
System.out.println("Output");
System.out.println("======");
for(i=0;i<c;i++)
{
f=isPrime(n); // Returing 1 if Prime or 0 if not prime
if(f==1)
System.out.println(n);
else
{
System.out.println("Not a Circular Number");
break;
}
// Example 113 113 -> 131 -> 311
a=n%((int)Math.pow(10,c-1)); // Reminder 13
b=n/((int)Math.pow(10,c-1)); // quotient 1
m=a*10+b; // here m = 13 * 10 + 1 =131
n=m; // then n = 131 this process repeated till the loop end
}
if(f==1)
System.out.println("Circular Prime");
}
// Prime checking method which return 1 if prime else 0
int isPrime(int x)
{
f1=1;
for(j=2;j<x;j++)
{
if(x%j==0)
{
f1=0;
break;
}
}
return f1;
}
// Main method
public static void main(String args[])
{
int n,m,c,f,i;
Scanner sc = new Scanner(System.in);
System.out.println("Enter a number :");
n= sc.nextInt();
CircularPrime ob=new CircularPrime();
ob.circularPrime(n);
}
}