Results 1 to 6 of 6
  1. #1
    soccergirl67 is offline Member
    Join Date
    Dec 2011
    Posts
    1
    Rep Power
    0

    Default 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!

  2. #2
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,800
    Rep Power
    7

    Default 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.

  3. #3
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,814
    Rep Power
    25

    Default 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.

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,733
    Blog Entries
    7
    Rep Power
    21

    Default 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:

    Java 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
    cenosillicaphobia: the fear for an empty beer glass

  5. #5
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,574
    Rep Power
    12

    Default 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...)

  6. #6
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,733
    Blog Entries
    7
    Rep Power
    21

    Default Re: Palindromic Prime?

    Quote Originally Posted by pbrockway2 View Post
    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
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. Prime Factorization
    By skaterboy987 in forum New To Java
    Replies: 7
    Last Post: 10-27-2011, 02:18 AM
  2. How to print palindromic numbers on the console!
    By AlfieSDK in forum New To Java
    Replies: 6
    Last Post: 05-21-2011, 03:24 AM
  3. Prime Number - System print all the prime numbers ...
    By pinkdreammsss in forum New To Java
    Replies: 20
    Last Post: 04-26-2009, 02:50 AM
  4. Prime numbers
    By tercius in forum New To Java
    Replies: 3
    Last Post: 05-04-2008, 07:05 AM
  5. Prime numbers
    By gapper in forum New To Java
    Replies: 3
    Last Post: 02-07-2008, 11:09 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •