dyertkd
dyertkd

Reputation: 17

Displaying Palindromic Prime number

I am trying to make a program to display the first 50 prime palindromes with 10 numbers per line. This is the code i have so far, however when run nothing happens. I have looked t similar solutions and can't seem to find were the error is. Any help would be appreciated.

 import java.lang.Math;
public class PalindromicPrime {
    public static void main(String[] args) {
        int counter = 1;
        int start = 2;      

        isPalindrome(start);
        isPrime(start);

        while (counter <= 50) {
            if (isPrime(start) && isPalindrome(start)) {
                System.out.print(start + " ");
                if (counter % 10 == 0) {
                    System.out.println();
                    counter++;
                }
                start++;
            }
        }
    }
    public static boolean isPalindrome(int x) {
        int reverse = 0;
        while(x > 0) {
        reverse = reverse * 10 + x % 10;
        x = x / 10;
        }
        if (reverse == x) {
            return true;
        }
        else {
            return false;
        }       
    }
    public static boolean isPrime(int x) {
        if (x % 2 == 0 && x != 2) {
            return false;
        }

        int sqr = (int)Math.sqrt(x);
        for (int i = 3; i <= sqr; i += 2) {
            if(x % i == 0) {
                return false;
            }
        }
        return true;
    }

}

Upvotes: 0

Views: 2772

Answers (3)

bw0248
bw0248

Reputation: 711

First of all as others have said your isPalindrome() Method is not working correctly.
I would suggest you just convert your int to a string and then check whether that is a Palindrome. I think it is the easiest way. Maybe others can comment whether that is a bad idea in terms of performance.
Here is how I would do it:

public static boolean isPalin(int x) {
    String s = Integer.toString(x);
    for(int i = 0; i < s.length()/2; i++) {
        if(s.charAt(i) != s.charAt(s.length()-i-1)) {
            return false;
        }
    }
    return true;
}

Also your while loop is not working correctly as you only increment start when you actually found a prime. Counter should be incremented everytime you found a prime.
On top of that you should base the condition for the while loop on your start value and not the counter for the line breaks.
Edit: Actually you should use counter in the while condition. I was wrong.

Here is the updated code:

public static void main(String[] args) {
    int counter = 0;  
    int start = 2;
    while (counter < 50) {
        if (isPrime(start) && isPalin(start)) {
            System.out.print(start + " ");
            counter++;
            if (counter % 10 == 0) {
                System.out.println();
            }
        }
        start++;
    }
}

public static boolean isPalin(int x) {
    String s = Integer.toString(x);
    for(int i = 0; i < s.length()/2; i++) {
        if(s.charAt(i) != s.charAt(s.length()-i-1)) {
            return false;
        }
    }
    return true;
}
public static boolean isPrime(int x) {
    if (x % 2 == 0 && x != 2) {
        return false;
    }

    int sqr = (int)Math.sqrt(x);
    for (int i = 3; i <= sqr; i += 2) {
        if(x % i == 0) {
            return false;
         }
     }
     return true;
}

Here is the output for the first 50 primes that are palindromic:

2 3 5 7 11 101 131 151 181 191 
313 353 373 383 727 757 787 797 919 929 
10301 10501 10601 11311 11411 12421 12721 12821 13331 13831 
13931 14341 14741 15451 15551 16061 16361 16561 16661 17471 
17971 18181 18481 19391 19891 19991 30103 30203 30403 30703 

Upvotes: 0

Jordan
Jordan

Reputation: 2283

  1. You're not incrementing start when it isn't prime, so you hit an infinite loop when you hit your first non-prime number. Put your start++ outside of the if statement.

  2. Your isPalindrome() method is broken. The variable x is whittled down to create reverse, but then you compare reverse to the modified version of x instead of its original value.

  3. You're only incrementing counter every 10th prime, so this will end up printing 500 palindromic primes, not 50.

Bonus: Finding primes is faster if you store every prime that you find, and then only check division by previously-found primes.

Upvotes: 1

Adrien
Adrien

Reputation: 497

Your code is an Infinite looping. This is because you increment start in the if statement, so only when start is a prime and palindrome number. If start isn't palindrome or prime, it will not enter the conditional and thus counter will Nevers incréée and reach 50

Upvotes: 0

Related Questions