Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Need help with emirp problem.

  1. #1
    Junior Member
    Join Date
    Apr 2011
    Posts
    5
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Need help with emirp problem.

    I have two problems. First, the output has single digit numbers in it which I believe makes them not emirps (because they are the same backwards and forwards). The second problem I'm having is in formatting. I tried several variations of the printf(%3d, n) variety and could not get them to format properly. It has to be the 1st 100 emirps, 10 per line, each row right justified. The example output is:

    13 17 31 37 71 73 79 97 107 113
    149 157 167 179 199 311 337 347 359 389

    Any help would be greatly appreciated.


  2. #2
    Junior Member
    Join Date
    Apr 2011
    Posts
    5
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Need help with emirp problem.

    Forgot to put my code in there:


    public class Exercise05_27 {

    public static void main (String [] args) {
    int count=0;
    int n=2;
    while (count<100){
    if (isEmirp(n)){
    System.out.print(n+" ");
    count++;
    if (count%10==0)System.out.println();
    }
    n++;
    }
    }

    public static boolean isPrime(int n){
    for(int i=2; i<=n/2; i++){
    if(n%i==0)return false;
    }
    return true;
    }
    public static boolean isEmirp(int n){
    return isPrime(n) && isPrime(reverseDigits(n));
    }

    public static int reverseDigits(int n){
    if (n<10) return n;
    return recursiveHelper(n%10,n/10);
    }
    public static int recursiveHelper(int p, int q){
    if(q<1) return p;
    return recursiveHelper(p*10+q%10,q/10);
    }

    }

  3. #3
    Junior Member
    Join Date
    Apr 2011
    Posts
    5
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Need help with emirp problem.

    Okay, I just looked at the output I typed in and it isn't formatted the way I typed it. It should be all lined up, 10 per line, each column right justified.