# Palindromic Prime?

• 12-01-2011, 12:23 AM
soccergirl67
Palindromic Prime?
Hello everyone at Java Forums!
My name is Cindy, I'm a sophomore at CSUN. This semester I decided to take a computer science class and I have no idea what I'm doing. For the first part of the class everything was going well until we reached loops and methods. Which is why I'm here today, my teacher assigned us a program that is supposed to check if a input number is a palindromic prime and then print out all palindromic primes between 100-500. (Palindromic primes are prime numbers that when reversed are the same number, 131, 313, 757 etc.)

From what i understand about the program, it needs to have 4 methods. Main, prime, reverse and palindromic prime. Any help that someone could offer would be a life saver!

Thanks a ton, Cindy!
• 12-01-2011, 12:39 AM
Junky
Re: Palindromic Prime?
Where is your code? What problems are you having? Do you have error messages?

Nobody is going to write the code for you. What you need to do is make an attempt and if you get stuck post your code, full error messages and ask a specific question. That way we can provide a specific answer.
• 12-01-2011, 02:26 AM
Norm
Re: Palindromic Prime?
Take the methods one at a time. Write the code, test it, fix it, test it, fix it etc until it works, Move on to the next method and repeat the process. Continue until done.
• 12-01-2011, 08:58 AM
JosAH
Re: Palindromic Prime?
A bit of thinking beforehand never hurts: the number can't be a number in the 200 or 400 range because the reverse of such a number would be even and never can be a prime number; so the number has to start with a 1 or a 3. The digit in the middle position can be anything. A nest loop is enough to create all such palindromes:

Code:

```for (int h=1; h < 5; h+= 2)   for (int t= 0; t <= 9; t++) {       int p= 101*h+10*t;       ...   }```
Number p is guaranteed to be a palindrome (because we constructed it that way) and isn't in the range 200 ... 299 nor in the range 400 ... 499; all we have to check is whether p is a prime number or not ...

kind regards,

Jos
• 12-01-2011, 09:38 AM
pbrockway2
Re: Palindromic Prime?
if((2*h+t)%3==0) continue; // eliminate the "bad" middle digits

(Although the most practical and useful questions and suggestions have already been made by Junky...)
• 12-01-2011, 10:48 AM
JosAH
Re: Palindromic Prime?
Quote:

Originally Posted by pbrockway2
if((2*h+t)%3==0) continue; // eliminate the "bad" middle digits

(Although the most practical and useful questions and suggestions have already been made by Junky...)

That sure is another nice bounding criterium; my enumeration leaves 20 potential palindromic primes and your condition cuts off another six or so, leaving 14 numbers to be tested (out of the original 400) ...

kind regards,

Jos