㈠ java實現1000以內迴文素數(質數)
publicclass${
publicstaticvoidmain(String[]args){
for(inti=1;i<=1000;i++){
if(!check(i)){
continue;
}
if(huiweishu(i)){
System.out.println(i);
}
}
}
privatestaticbooleancheck(intnum){
intsqrt=(int)Math.sqrt(num);
for(intj=2;j<sqrt+1;j++){
if(num%j==0){
returnfalse;
}
}
returntrue;
}
privatestaticbooleanhuiweishu(inti){
inta;
if(i<99){
a=i/10;
}else{
a=i/100;
}
returna==i%10;
}
}
11
101
131
151
181
191
313
353
373
383
727
757
787
797
919
929
㈡ 鍙嶇礌鏁幫紙閫嗗悜鎷煎啓鐨勭礌鏁幫級緙栧啓java紼嬪簭
public class TempCls
{
public static boolean isPrime(long n)
{ //鍒ゆ柇緔犳暟
int i = 2;
while (i*i <= n)
{
if (n % i ==0)
return false;
i++;
}
return true;
}
public static String reverseStr(String str)
{ //鍙嶈漿瀛楃︿覆
String result = "";
for (int i = str.length()-1; i >= 0; i--)
{
result += str.charAt(i);
}
return result;
}
public static boolean reverseJudge(long n)
{ //鍙嶈漿鏁板瓧鍒ゆ柇
String nStr = reverseStr(n + "");
return isPrime(Integer.parseInt(nStr));
}
public static boolean isPalindromic(long n)
{ //鍥炴枃鍒ゆ柇
String str1 = n + "";
String str2 = reverseStr(str1);
return !str1.equals(str2);
}
public static void main(String[] args)
{
int n = 0; //璁℃暟鍣
int i = 2; //1涓嶆槸緔犳暟錛屾晠浠2寮濮
while (n < 100)
{
if (isPalindromic(i) && isPrime(i) && reverseJudge(i))
{
System.out.print(i + "\t\t");
n++;
if (n % 10 == 0)
System.out.println();
}
i++;
}
}
}
㈢ 鍥炴枃緔犳暟鏄浠涔
浠庡乏鍚戝彸璇諱笌浠庡彸鍚戝乏璇繪槸瀹屽叏涓鏍風殑錛岃繖鏍風殑鏁扮О涓衡滃洖鏂囨暟鈥濄傚洖鏂囨暟涓瀛樺湪鏃犵┓澶氫釜緔犳暟11錛101錛131錛151錛191鈥︹︺傞櫎浜11浠ュ栵紝鎵鏈夊洖鏂囩礌鏁扮殑浣嶆暟閮芥槸濂囨暟銆傞亾鐞嗗緢綆鍗曪細濡傛灉涓涓鍥炴枃緔犳暟鐨勪綅鏁版槸鍋舵暟錛屽垯瀹冪殑濂囨暟浣嶄笂鐨勬暟瀛楀拰涓庡伓鏁頒綅涓婄殑鏁板瓧鍜屽繀鐒剁浉絳夛紱鏍規嵁鏁扮殑鏁撮櫎鎬х悊璁猴紝瀹規槗鍒ゆ柇榪欐牱鐨勬暟鑲瀹氳兘琚11鏁撮櫎錛屾墍浠ュ畠灝變笉鍙鑳芥槸緔犳暟銆
鏈榪戝彂鐜扮殑鏈澶х殑鍗佷釜鍥炴枃緔犳暟:
742950290870000078092059247, 742950290871010178092059247,742950290872020278092059247, 742950290873030378092059247,742950290874040478092059247, 742950290875050578092059247,742950290876060678092059247, 742950290877070778092059247,742950290878080878092059247, 742950290879090978092059247