如果一个整数只能被1和自己整除,就称这个数是素数。
如果一个数正着反着都是一样,就称为这个数是回文数。例如:6, 66, 606, 6666
如果一个数字既是素数也是回文数,就称这个数是回文素数
牛牛现在给定一个区间[L, R],希望你能求出在这个区间内有多少个回文素数。
输入包括一行,一行中有两个整数(1 ≤ L ≤ R ≤ 1000)
输出一个整数,表示区间内回文素数个数。
100 150
2
import java.util.Scanner; /** * @Author: coderjjp * @Date: 2020-05-14 9:02 * @Description: 回文素数 * @version: 1.0 */ public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int L = sc.nextInt(), R = sc.nextInt(); int ans = 0; for (int i = L; i <= R; i++) if (isOK1(i) && isOK2(i)) ans++; System.out.println(ans); } private static boolean isOK2(int i) {//判断素数 if (i == 1) return false; for (int j = 2; j*j <= i; j++) if (i % j == 0) return false; return true; } private static boolean isOK1(int i) {//判断回文 int temp = i; int j = 0; while (temp > 0){ j = j * 10 + temp % 10; temp /= 10; } return i == j; } }
import java.util.Scanner; public class Question18 { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int l = sc.nextInt(); int r = sc.nextInt(); int count =0; for(int i=l;i<=r;i++){ if(i==1){ count--; } if(sushu(i)&&huiwenshu(i)){ count++; } } System.out.println(count); } public static boolean sushu(int i){ for(int j =2 ;j<i;j++){ if(i%j ==0){ return false; } } return true; } public static boolean huiwenshu(int i){ char[] arr = (i+"").toCharArray(); int[] arr1 =new int[arr.length]; for(int k = 0;k<arr.length;k++){ arr1[k] =(int)(arr[k]); } int left =0; int right =arr.length-1; while(left<=right){ if(arr1[left] == arr1[right]){ left++; right--; }else { return false; } } return true; } }
public static void main(String[] args){ Scanner scan = new Scanner(System.in); int L = scan.nextInt(); int R = scan.nextInt(); int count = 0; for(int i=L; i<=R; i++){ if(isPalindromeAndPrime(i)){ count++; } } System.out.println(count); } public static boolean isPalindromeAndPrime(int a){ if(a == 1)//1不是质数 return false; for(int i=2; i<a; i++){//判断是否为质数 if(a%i == 0){ return false; } } String s = a + ""; int i = 0, j = s.length()-1; while(i < j){//判断是否为回文数 if(s.charAt(i) != s.charAt(j)){ return false; } i++; j--; } return true; }
import java.util.*; public class Main{ public static void main(String[] args){ try(Scanner in = new Scanner(System.in)){ System.out.println(helper(in.nextInt(),in.nextInt())); } } public static int helper(int l,int r){ int count = 0; if(l == 1) l++;//1不是素数 while(l <= r){ if(isHuiWen(l) && isSu(l)) count++; l++; } return count; } public static boolean isSu(int num){ int i = 2; while(i * i <= num){ int j = num / i; if(j * i == num) return false; i++; } return true; } public static boolean isHuiWen(int num){ char[] cs = Integer.toString(num).toCharArray(); int i = 0,j = cs.length - 1; while(i <= j){ if(cs[i] != cs[j]) return false; i++; j--; } return true; } }