The method returns a string as hours minutes seconds For exa

The method returns a string as hours: minutes: seconds. For example, convertMill is(5500) returns a string 0:0:5, convertMill is (100000) returns a string 0:1:40, and convertMill is(555550000) returns a string 154:19:10. (Palindromic prime) 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. Write a program that displays the first 100 palindromic prime numbers. Display 10 numbers per line, separated by exactly one space, as follows: (Emirp) An emirp (prime spelled backward) is a nonpalindromic prime number whose reversal is also a prime. For example, 17 is a prime and 71 is a prime, so 17 and 71 are emirps. Write a program that displays the first 100 emirps. Display 10 numbers per line, separated by exactly one space, as follows:

Solution

PalindromicPrime.java


public class PalindromicPrime {

  
   public static void main(String[] args) {
       int count = 0;
       int numOfPrimePalin = 0;
       for(long i=2;; i++){
           if(isPrime(i) && isPalindrome(i)){
           System.out.print(i+\" \");
           count++;  
           numOfPrimePalin++;
           if(count == 10){
               count = 0;
               System.out.println();
           }
          
           if(numOfPrimePalin == 100){
               break;
           }
           }
       }

   }
   public static boolean isPrime(long n) {
   for (long f = 2; f <= n / 2; f++) {
   if (n % f == 0) {
   return false;
   }
  
   }
   return true;
   }
       public static boolean isPalindrome(long i){
   long m = 0;
   long n = i;
   long l = 0;

   while (n > 0) {
   l = n % 10;
   m = m * 10 + l;
   n = n / 10;
   }
   if(m == i)
   return true;
   else
   return false;
       }     

}

Output:

2 3 5 7 11 101 131 151 181 191
313 353 373 383 727 757 787 797 919 929
10301 10501 10601 11311 11411 12421 12721 12821 13331 13831
13931 14341 14741 15451 15551 16061 16361 16561 16661 17471
17971 18181 18481 19391 19891 19991 30103 30203 30403 30703
30803 31013 31513 32323 32423 33533 34543 34843 35053 35153
35353 35753 36263 36563 37273 37573 38083 38183 38783 39293
70207 70507 70607 71317 71917 72227 72727 73037 73237 73637
74047 74747 75557 76367 76667 77377 77477 77977 78487 78787
78887 79397 79697 79997 90709 91019 93139 93239 93739 94049

 The method returns a string as hours: minutes: seconds. For example, convertMill is(5500) returns a string 0:0:5, convertMill is (100000) returns a string 0:1:
 The method returns a string as hours: minutes: seconds. For example, convertMill is(5500) returns a string 0:0:5, convertMill is (100000) returns a string 0:1:

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site