Printing Emirp Numbers in Java

Problem:

An emirp (prime spelled backward) is a nonpalindromic prime number whose reversal is also a prime. For example, 17 is a prime and 71 is a prime. So, 17 and 71 are emirps. Write a program that displays the first 100 emirps. Display 10 numbers per line and align the numbers properly. as follows:


Output:



Solution:

 //Happy note: based on our input, it's okay if we duplicate
 //palindromes, i.e. we can have both 17 and 71 among the 100
 //that means we have to code less than we would've if it
 //only wanted sets of palindromes
 import java.util.Scanner;  
 public class Emirp
 {  
        
      //To make things easier, let's just make
      //a method to determine if a number is a
      //prime or not instead of copying the code
      //into our loops
      public static boolean isPrime(int N)  
      {  
           //Here's a trick about numbers and their divisors:
           //We all know that a number like a number n can have 
           //as finite divisors 1, itself, and any number between
           //1 and n that divides n withou a remainder.
           //To find out if a number n has a divisor greater than 1
           //and less than itself (i.e., not prime) we can go throu
           //every number between 1 and n and check if we find at least
           //one divisor. But a shortcut to that is to check every 
           //number between 1 and n squared. For some reason
           //this is a valid math theorem, so just work with it
           //cz it'll involve less looping :)
           for(int i =2;i<=Math.sqrt(N);i++)   
           {  
                if(N%i==0)   
                {  
                     //Of course if we found out that it does
                     //divide n, then it is not prime so we should
                     //stop this method automatically
                     return false;  
                }  
           }  
           //And if we found nothing between 1 and n squared
           //then n truly is a prime
           return true;  
      }  
        
      //Just like we did with primeness
      //let's make a method for palindromes 
      //to save us time
      //All we need to input is the number itself
       public static int reverse (int N)  
      {  
           String str = Integer.toString(N);  
           String targetS = "";  
           {  
                for(int i =str.length()-1;i>=0;i--)  
                {  
                     targetS+= str.charAt(i);  
                }  
           }  
           return Integer.parseInt(targetS);  
      }  
     
      //Note: we want to get a NON-palindrome prime,
      //the best way to do this is to first check if 
      //a number is a prime and then seeing if it IS 
      //a palindrome. It's easier to make a method that
      //shows that it's prime than the converse. 
      

      //There are two common ways of testing palindromes
      //One is comparing and looping between each side of a word
      //through variables left and right, and the other method
      //is to create a string that has the reverse of our word
      //and comparing it with the original to see if the original
      //string is the same as its reverse
      //We'll use the second technique here just for convenience
      //and first try to find the reversed form
      //Note: we could've integrated this with the next palindrome method
      //but it's just always more neater to work with sets of methods
      //instead of just one method
      public static int reverse (int N)  
      {  
           //However, to reverse the digits in a int
           //we have to convert it into a String 
           //so that we can treat it like a set of characters
           String str = Integer.toString(N);  
 
          //Note: we can't remove things from a string variable
          //but just add, so we have to make a new empty String and add
          //to it the characters of our original string but in reverse
           String targetS = "";  
           
           //Since we're to reverse the original String
           //then we have to start from the last index/character
           //and make ourselves go to the left, while adding everything
           for(int i =str.length()-1;i>=0;i--)  
           {  
               targetS+= str.charAt(i);  
           }  
           //Since we want to return an int (cz we are working with
           //numbers) then we have to convert our reversed String
           //into an int variable with the following method:
           return Integer.parseInt(targetS);  
      }  

      //The reverse and prime methods are useless if there's no method
      //that will use them to see if is prime AND a palindrome
      //or not, so it's time for: 
      public static boolean isPalindromicPrime(int N)  
      {  
           //As expected, we need to convert to string
           String S = Integer.toString(N);  
           if (isPrime(N))  
           {  
                //Note: to convert a int or anything into a String, we can 
                //either use a method (Integer.toString(N), etc) or just append
                //our int to ""; both techniques work
                if(N.equalsIgnorecase(""+reverse(N))
                    return true
           }  
           else
                return false;  
      }  

      //Time to see if our N is prime, not a palindrome,
      //but has a prime number as a reversal
      public static boolean isEmirp(int N)  
      {  
           String S = Integer.toString(N);
           //Hint: always separate conditions with () in case you have
           //a condition that uses operators just to be safe/clear  
           if (isPrime(N) && isPrime(reverse(N)) && (isPalindromicPrime(N) == false))  
                return true;  
           else  
                return false;  
      }  
        
      //God... that was a lot, but we need our 100  
      public static void main (String[] args)  
      {  
           //We want the first 100 emirps so lets make an array
           //to store them 
           int[] pprime = new int[100];  
           //We need something to count how many emirps we've found
           int count =0;  
           //And now it's time to loop over every number above 2 to
           //find our emirps and store them till we reach the 100 count
           for (int pal =2;count<100;pal++)  
           {  
                if (isEmirp(pal))  
                {  
                     pprime[count] = pal;  
                     count++;  
                }   
           }
           
           //...We still gotta print out our 100 emirps     
           for(int i =0;i<100;i++)  
           {  
                //Well, we need to make it look like a table and
                //a table needs to have an ending and we need to start
                //a new line after every 10th emirp
                if ((i+1) % 10 == 0) 
                     System.out.println(pprime[i]);  
                else 
                     System.out.print(pprime[i] + "\t");  
           }  
      }  
        
   
 }  


1 comment :

  1. on line 113, when I compile this code I get the error "int cannot be dereferenced". How does this get resolved properly?

    ReplyDelete

Follow Me

If you like our content, feel free to follow me to stay updated.

Subscribe

Enter your email address:

We hate spam as much as you do.

Upload Material

Got an exam, project, tutorial video, exercise, solutions, unsolved problem, question, solution manual? We are open to any coding material. Why not upload?

Upload

Copyright © 2012 - 2014 Java Problems  --  About  --  Attribution  --  Privacy Policy  --  Terms of Use  --  Contact