简体   繁体   中英

Why is my program not continuing after the first 10 Palindromic numbers? And also Why is my isPrime method not returning false for the number 4?

I'm not sure why my ct is not going all the way to 100 even though I clearly set it to go until it reaches 100.

public class PalindromicPrime
{
   public static void main(String [] args)
   {     


        int ct = 0;
          while(ct < 100)
          {
             if(isPalindrome(ct) && isPrime(ct))
             {
                if(ct % 10 != 0)
                {
                   System.out.print(ct + " ");
                }
                else
                {
                   System.out.print("\n");
                }
             }
             ct++;
          }
       public static boolean isPalindrome(int p) 
       {
          int palindrome = p;
          int reverse = 0;
          while (palindrome != 0) 
          {
             int remainder = palindrome % 10;
             reverse = reverse * 10 + remainder;
             palindrome = palindrome / 10;
          }
          if (p == reverse) 
          {
             return true;
          }
            return false;
       }

I'm assuming my isPrime code is wrong since I'm getting a 4 in my output. What's wrong with this method?

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

}

First change you should do in your method isPrime() is change this line

for(int i = 2; i < p/2; i++)

to

for(int i = 2; i <= p/2; i++) // you can easily find the reason why it is necessary(=)

And also you are printing palindromic numbers less than 100 which are prime,not first 100 palindrome numbers, if you want to print first 100 palindrome numbers you can take another counter which will keep track of the numbers printed.

You can modify your main method like this:

public static void main(String [] args)
   {     
        int ct = 0,n=0; // n to count numbers printed/found
          while(n < 100) // change the condition to check n<100
          {
             if(isPalindrome(ct) && isPrime(ct))
             {
                System.out.print(ct + " ");
                if(n % 10 == 0)
                {
                   System.out.println();
                }
                n++; // incementing n after a number is found!
             }
             ct++;
          }
    }

Change your isPrime function to following (replace < with <= as 4/2 is 2 and loop will not run at all for p=4):

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

public static void main(String[] args) {
    int ct = 2;
    int count = -1;
    while (count < 99) {
        if (isPalindrome(ct) && isPrime(ct)) {
            count++;
            if (count % 10 == 0) {
                System.out.print("\n" );
            }   
                System.out.print(ct + " ");
        }
        ct++;
    }
}

The only numbers that are palindrome and prime and less than 100 are:

1 2 3 5 7 11

Try changing the value of 100 to 102. Then you get the following output as 101 is the next palindromic prime after 11:

1 2 3 5 7 11 101 

Your palindrome method is fine. It's your isPrime method that's not working because to check if a number is prime, you're supposed to test factors up to the square root of the number. So a simple change in the condition should do it,

public static boolean isPrime(int p)
       {
          for(int i = 2; i <= Math.sqrt(p); i++)
          {
             if(p % i == 0)
             {
                return false;
             }
          }
          return true;
       }

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM