如果一个整数只能被1和自己整除,就称这个数是素数。
如果一个数正着反着都是一样,就称为这个数是回文数。例如:6, 66, 606, 6666
如果一个数字既是素数也是回文数,就称这个数是回文素数
牛牛现在给定一个区间[L, R],希望你能求出在这个区间内有多少个回文素数。
输入包括一行,一行中有两个整数(1 ≤ L ≤ R ≤ 1000)
输出一个整数,表示区间内回文素数个数。
100 150
2
//可以先把1000以内所有回文素数求出来,
//写成一个数组去搜索,这样笔试时候绝对是最快的
#include<iostream>
#include<vector>
using namespace std;
bool IsPrime(int x)
{
if (x == 0 || x == 1) return false;
for (int i = 2; i <= x / 2; i++)
if (x % i == 0)
return false;
return true;
}
bool IsRevrse(int x)
{
int temp = 0,xs=x;
while (x)
{
temp = (temp * 10 + x % 10);
x = x / 10;
}
return xs == temp ? true : false;
}
int main()
{
int l, r, cnt = 0;
cin >> l >> r;
for (int i = l; i <= r; i++)
if (IsRevrse(i) && IsPrime(i))
cnt++;
cout << cnt << endl;
return 0;
}
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; } }
#include<stdio.h> bool isp(int x){ if(x==1) return false; for(int i=2;i<x;i++) if(!(x%i)) return false; return true; } bool judge(int x){ int a[1000],c=0,i,j; while(x) a[c++]=x%10,x/=10; for(i=0,j=c-1;i<j;i++,j--) if(a[i]!=a[j]) return false; return true; } int main(){ int l,r,i,res=0; for(scanf("%d%d",&l,&r),i=l;i<=r;i++) if(isp(i)&&judge(i)) res++; printf("%d",res); }
import math
# 判断n是否为回文数且为素数
def is_prime_and_palindrome(n):
if str(n) != str(n)[::-1]:
return False
if n <= 1:
return False
for i in range(2, int(math.sqrt(n)) + 1):
if n % i == 0:
return False
return True
start, end = map(int, input().split())
print(len(list(filter(lambda c: is_prime_and_palindrome(c), range(start, end + 1)))))
//暴力 #include<iostream> #include<stdio.h> #include<algorithm> #include<string> #include<vector> #include<math.h> using namespace std; //是否回文 bool judgeHuiWen(int a){ string stra = to_string(a); if(stra.size() == 1){ return true; } for(int i = 0, j = stra.size() - 1; i < j; i++,j--){ if(stra[i] != stra[j]){ return false; } } return true; } //是否素数 bool judgePrime(int a){ if(a <= 1){ return false; } for(int i = 2; i <= sqrt(a); i++){ if(a % i == 0){ return false; } } return true; } int main(){ int L, R; cin >> L >> R; //看他是否回文,再看他是否素数 int count = 0; for(int i = L; i <= R; i++){ if(judgeHuiWen(i) && judgePrime(i)){ count ++; } } cout << count << endl; }
L, R = [int(i) for i in input().strip().split()] count = 0 for n in range(L, R+1): if str(n) == str(n)[::-1] and n % 2 != 0: for d in range(3, n, 2): if n % d == 0: count -= 1 break count += 1 print(count) # 运行时间:31ms # 占用内存:3432k
using System; public class main { public static bool Prime(int n) { if (n > 1) { for (int i=2; i<=Math.Floor(Math.Sqrt(n)); i++) if (n%i == 0) return false; return true; } return false; } public static bool Huiwen(int n) { int temp = n, s = 0; while (temp > 0) { s = s*10+temp%10; temp /= 10; } if (s == n) return true; return false; } public static void Main(String[] args) { String[] scan = new String[2]; scan = Console.ReadLine().Split(' '); int l = Convert.ToInt32(scan[0]), r = Convert.ToInt32(scan[1]); int i, s = 0; for (i=l; i<=r; i++) { if ((Huiwen(i) == true) && (Prime(i) == true)) s++; } Console.WriteLine(s); Console.ReadKey(); return; } }
def judge(l, r): res = 0 for num in range(l, r+1): if num > 1: for i in range(2, num//2+1): if num%i == 0: break # 在这种结构里,一定要使用break,countinue达不到效果 else: num = str(num) for j in range(len(num)//2): if num[j] != num[len(num)-j-1]: break else: res += 1 return res if __name__ == "__main__": l, r = map(int, input().split()) print(judge(l, r))
# 1.1不是素数 2.考虑输入区间可能是一个数的情况。 def su(n): if n==1: return 0 for i in range(2, n): if n%i==0: return 0 return 1 def hui(n): a = str(n) b = a for i in range(len(a)-1): if a[i]!=b[len(a)-i-1]: return 0 return 1 a = list(map(int,input().split())) count = 0 for i in range(a[0], a[1]+1): if su(i) and hui(i): count+=1 print(count)
#include<iostream> #include<algorithm> using namespace std; bool isPrime(int n){ if(n==1) return 0; for(int i=2;i<=sqrt(n);i++){ if(n%i==0) return 0; } return 1; } int main(){ int a,b,count=0; cin>>a>>b; for(int i=a;i<=b;i++){ if(isPrime(i)){ int temp=i,reverse=0; while(temp>0){ reverse=reverse*10+temp%10; temp/=10; } if(reverse==i) count++; } } cout<<count<<endl; }
#include <bits/stdc++.h> using namespace std; bool isPrime(int n){ if(n==1) return false; if(n==2) return true; for(int i=2;i*i<=n;i++) if(n%i==0) return false; return true; } bool F(int n){ string s = to_string(n); int m = s.length(); for(int i=0,j=m-1;i<j;i++,j--) if(s[i]!=s[j]) return false; return true; } int main() { int L,R,a[1000]={0}; for(int i=1;i<=1000;i++) if(isPrime(i) && F(i)) a[i] = a[i-1]+1; else a[i] = a[i-1]; while(cin>>L>>R) cout<<a[R]-a[L-1]<<endl; return 0; }
# coding=utf-8 # 判断一个数是否为素数 def Prime_number(n): if n%2 == 0 : return False else: for i in range(3,n,2): if n%i == 0: return False return True # 判断一个数是否为回文数 def Palindrome(n): if str(n) == str(n)[::-1]: return True else: return False def fun(): s = map(int,raw_input().split()) L,R = s[0],s[1] count = 0 for i in range(L,R+1): if Prime_number(i) and Palindrome(i): count += 1 return count if __name__=='__main__': print(fun())
import java.util.Scanner; public class Main { public static void main(String[] args){ Scanner scanner = new Scanner(System.in); int start = scanner.nextInt(); int end = scanner.nextInt(); System.out.println(Main.method(start,end)); } private static int method(int start,int end){ int count = 0; for (int i = start; i <= end; i++) { String str = i + ""; String str2 = new StringBuilder(str).reverse().toString(); if (str2.equals(str)&&isPrime(Integer.parseInt(str))) { count++; } } return count; } private static boolean isPrime(int i) { if (i < 2) return false; for (int j = 2; j <= Math.sqrt(i); j++) { if (i % j == 0){ return false; } } return true; } }
def prime(n): for i in range(2, int(n**0.5)+2): if n % i == 0: return False return True def palin(n): return str(n) == str(n)[::-1] l, r = map(int, input().split()) ans = 0 for i in range(l, r+1): if prime(i) and palin(i): ans += 1 print(ans)
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; } }