Java Method definition calculate palindromic prime numbers

Question

A palindromic prime is a prime number and also palindromic.

For example, 131 is a prime and also a palindromic prime, as are 313 and 757.

We would like to write a program that displays the first 100 palindromic prime numbers.

What is the output of the following code?

public class Main {
  public static void main(String[] args) {
    int count = 1;

    for (int i = 2; true; i++) {
      if (isPrime(i) && isPalindrome(i)) {
        System.out.print(i + " ");
        if (count % 10 == 0) {
          System.out.println();/* w w w. jav a 2s  .  co m*/
        }
        if (count == 100) {
          break;
        }
        count++; // Increase count
      }
    }
  }
  //your code here
}




public class Main {
  public static void main(String[] args) {
    int count = 1;

    for (int i = 2; true; i++) {
      if (isPrime(i) && isPalindrome(i)) {
        System.out.print(i + " ");
        if (count % 10 == 0) {
          System.out.println();
        }
        if (count == 100) {
          break;
        }
        count++; // Increase count
      }
    }
  }

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

    return true;
  }

  /** Return the reversal of number */
  static int reversal(int number) {
    int result = 0;

    while (number != 0) {
      int lastDigit = number % 10;
      result = result * 10 + lastDigit;
      number = number / 10;
    }

    return result;
  }

  /** Return true if number is palindromic */
  static boolean isPalindrome(int number) {
    return number == reversal(number);
  }
}



PreviousNext

Related