简体   繁体   English

为什么在前10个回文数之后我的程序无法继续? 而且为什么我的isPrime方法没有为数字4返回false?

[英]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. 我不确定为什么我的ct不会一直达到100,即使我明确地将其设置为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. 我假设我的isPrime代码是错误的,因为我在输出中得到了4。 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 在方法isPrime()应该做的第一个更改是更改此行

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. 而且, 您正在打印的回文编号小于100的是质数,而不是前100个回文编号,如果要打印前100个回文编号,则可以使用另一个计数器来跟踪打印的编号。

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): 将您的isPrime函数更改为以下函数(将<替换为<=因为4/2是2,并且对于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: 回文数和素数且小于100的唯一数字是:

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: 尝试将100的值更改为102。然后将得到以下输出,因为101是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. 这是您的isPrime方法不起作用的原因,因为要检查数字是否为质数,您应该测试因子直至数字的平方根。 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;
       }

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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