Tuesday, June 11, 2013

Program to generate Palprime numbers(numbers which are both palindrome and prime)

PROGRAM

Palprime Numbers. Palprime numbers are the palindromic prime numbers i.e.,  the prime numbers that are palindrome also.
First palprime number is a 2-digit (2-d) number and it is 11. There is only one palprime, which is 2-digits long.
To generate a 3-digit(3-d) palprime number, there are many methods. One method is where one just needs to insert a 1-d palprime number in between 11 and then test for its primeness.
This way only two palprimes are generated(by inserting 3 and 5 between 11)
     131 and 151
(insertion of 7 and 11 does not yield a prime number)
You need to write the method GenPalPrime for the following  class that generates palprimes using the method mentioned here.
  public class PalPrime
{
  public static void GenPalPrime(int n)
  {
      /*The passed number n tells the number of digits in the palprime
        to be generated. Acceptable values for n are 2-5*/
   }
The 5-d palprimes may be generated by inserting some 3-d palprimes in 11 e.g., insertion of 131,383,797 yields these palprime numbers: 11311, 13831, 17971.
You may define additional methods, if needed.
Sample Input Output:
Enter the width of palprime numbers (ie no of digits in a palprime no):  2
11
Enter the width of palprime numbers (ie no of digits in a palprime no):  3
131

151
----------------------------------------------------------------------------------------------------------

/**
 * class PalPrime generates palprime numbers.
 * @author: Nitendra Verma 
 * @version: June 11,2013
 */
import java.util.Scanner;
public class PalPrime
{
  public static boolean isprime(int n) //method, checks whether a number is prime or not
  {
      int flag=1;
    for(int i=2;i<n;i++)  
    {
        if(n%i==0)
        {
            flag=0;
            break;
        }
    }
    if(flag==1)
      return true;
    else
      return false;
  }
  
  public static boolean ispal(int n)  //method, checks whether a number is palidrome or not
  {
      int n1,r1,ntemp,rev=0;
      ntemp=n;
      do
      {
          n1=n/10;
          r1=n%10;
          rev=rev*10+r1;
          n=n1;     
        }
        while(n1>0);
        if(rev==ntemp)
           return true;
        else
           return false;
  }

  
  public static void GenPalPrime(int n)  //method to generate numbers which are prime and palindrome both
  {
    int count=0;
    if(n==2)
    {
     System.out.println("11");
     count++;
    }
  else if(n==3)
   {
       int begnum=101;
       int lnum=0;
       boolean boolpr=false;
       boolean boolpl=false;
       for(int i=3;i<10;i++)
       {
           boolpr=isprime(i);
           boolpl=ispal(i);
           if(boolpr==true && boolpl==true)
           {
               lnum=begnum+i*10;
               if(isprime(lnum)==true && ispal(lnum)==true)
               {
               System.out.println(lnum);
               count++;
            }
            }
        }
    }
    
     else if(n==4)
   {
       int begnum=1001;
       int lnum=0;
       boolean boolpr=false;
       boolean boolpl=false;
       for(int i=10;i<100;i++)
       {
           boolpr=isprime(i);
           boolpl=ispal(i);
           if(boolpr==true && boolpl==true)
           {
               lnum=begnum+i*10;
               if(isprime(lnum)==true && ispal(lnum)==true)
               {
               System.out.println(lnum);               
               count++;
               }
            }
        }        
    }
    
    else if(n==5)
   {
       int begnum=10001;
       int lnum=0;
       boolean boolpr=false;
       boolean boolpl=false;
       for(int i=100;i<1000;i++)
       {
           boolpr=isprime(i);
           boolpl=ispal(i);
           if(boolpr==true && boolpl==true)
           {
               lnum=begnum+i*10;
               if(isprime(lnum)==true && ispal(lnum)==true)
               {
               System.out.println(lnum);
               count++;
            }
            }
        }       
    }
    else
     System.out.println("Value of n should be within range 2-5");
    if(n>=2 && n<=5 && count==0)
     System.out.println("No palprime number of this width");
}

 public static void main()
 {
     PalPrime ob=new PalPrime();  //creating object of class PalPrime
     Scanner in=new Scanner(System.in);  //creating object of class Scanner
     System.out.println("Enter the width of palprime numbers(i.e., no of digits in a palprime no):");
     int num=in.nextInt();
     GenPalPrime(num);  //method calling
 }
}

OUTPUT
Enter the width of palprime numbers(i.e., no of digits in a palprime no):
3
131
151


/*---------------Program developed by: Nitendra Verma---------------*/
//For more details visit http://javawithnitendra.blogspot.in

No comments:

Post a Comment

Ur comments r most welcome...