**6.26(回文素数)回文素数是指一个数同时为素数和回文数。例如:131是一个素数,同时也是一个回文素数。数学313和757也是如此。编写程序,显示前100个回文素数。每行显示10个数,数字中间用一个空格隔开。如下所示: 2 3 5 7 11 101 131 151 181 191 313 353 373 383 727 757 787 797 919 929 … **6.26(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 120 palindromic prime numbers. Display 10 numbers per line, separated by exactly one space, as follows: 2 3 5 7 11 101 131 151 181 191 313 353 373 383 727 757 787 797 919 929 …
参考代码:
package chapter06;publicclassCode_26{publicstaticvoidmain(String[] args){int palindPrimeCount =0;for(int i =2;palindPrimeCount !=100;i++){if(isPalindrome(i)&&isPrime(i)){
System.out.print(i+" ");
palindPrimeCount++;if(palindPrimeCount %10==0)
System.out.print("\n");}}}publicstaticintreverse(int number){int reverseNumber =0;do{
reverseNumber = reverseNumber *10+ number %10;
number /=10;}while(number >0);return reverseNumber;}publicstaticbooleanisPalindrome(int number){returnreverse(number)== number;}publicstaticbooleanisPrime(int number){for(int i =2;i <= Math.sqrt(number);i++)if(number % i ==0)returnfalse;returntrue;}}
结果显示:
2357111011311511811913133533733837277577877979199291030110501106011131111411124211272112821133311383113931143411474115451155511606116361165611666117471179711818118481193911989119991301033020330403307033080331013315133232332423335333454334843350533515335353357533626336563372733757338083381833878339293702077050770607713177191772227727277303773237736377404774747755577636776667773777747777977784877878778887793977969779997907099101993139932399373994049
Process finished with exit code 0