Description

The number 151 is a prime palindrome because it is both a prime number and a palindrome (it is the same number when read forward as backward). Write a program that finds all prime palindromes in the range of two supplied numbers a and b (5 <= a < b <= 100,000,000); both a and b are considered to be within the range .

Input

There are multiple test cases.

Each case contains two integers, a and b.

a=b=0 indicates the end of input.

Output

For each test case, output the list of palindromic primes in numerical order, one per line.

Sample Input
aaarticlea/jpeg;base64,/9j/4AAQSkZJRgABAQEAYABgAAD/2wBDAAgGBgcGBQgHBwcJCQgKDBQNDAsLDBkSEw8UHRofHh0aHBwgJC4nICIsIxwcKDcpLDAxNDQ0Hyc5PTgyPC4zNDL/2wBDAQkJCQwLDBgNDRgyIRwhMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjL/wAARCAARABIDASIAAhEBAxEB/8QAGAABAAMBAAAAAAAAAAAAAAAAAAMFBwT/xAAlEAACAQQCAQMFAAAAAAAAAAABAgMABAURBiESIjFBMjZxdbP/xAAYAQACAwAAAAAAAAAAAAAAAAAAAwEEBf/EABsRAQEAAgMBAAAAAAAAAAAAAAEAAgMEEyFh/9oADAMBAAIRAxEAPwDQeRW+SyVnctBIkiiScOk87qm0ciP0aZWA8dkEDZA2fcGPCWPI+PXkUt3GIcQjkyQxTGdtMrAhUVQO5CraVd/UB1pa7cnHmbaW5hjxEktoZJJGulnjChWYsT4lvLoHvr3B1vommvuQYaSe/jGSxrW9yXEiCWIiTe9eWohvs/LH8n5ocDh9jlnsER+zt+9wDE9G0uKWO4hSaGRJIpFDI6MCrKewQR7ilVfFPs7B/r4P5rStB8ZJW9KUqIlKUoi//9k=" alt="" /> Copy sample input to clipboard
5 500
0 0
Sample Output
5
7
11
101
131
151
181
191
313
353
373
383 题目链接:4190. Prime Palindromes
题意分析:求给定区间内的质数回文数
题目分析:
1.多组测试数据,所以先打表。
2.先求质数再判断回文,效率低下;所以先构造回文数,再判断质数。
3.偶数位的回文数都能被11整除,自己证明去。所以,偶数位的回文数除了11都是合数。
4.一个k位数,可以构造出一个奇数位的回文数。比如13,可以构造131;189可以构造18981.所以100000000内的只要从1构造到9999即可。
5.若范围为1000000000,那么明显超出int范围,要用long long。当然此题无此陷阱。

//http://soj.me/show_problem.php?pid=4190
#include <iostream>
#include <cstdio>
#include <vector>
#include <cmath>
#include <algorithm>
using namespace std; vector<int> v; bool prime(int x)
{
for (int i = ; i < sqrt(x+0.5); i++)
if (x%i == )
return ; return ;
} void get()
{
// 11是唯一的偶数位的质数回文数。
v.push_back(); //构造奇数位的回文数
for (int i = ; i <= ; ++i)
{
int tmp = i/, sum; for (sum = i; tmp != ; tmp /= )
{
sum = sum* + tmp%;
} if (prime(sum))
v.push_back(sum);
}
}
int main()
{
get();
sort(v.begin(), v.end()); //因为是不按顺序push,所以要sort
int a, b;
while(cin >> a >> b, a||b)
{
for (int i = ; i < v.size(); ++i)
{
if (v[i] >= a)
{
if (v[i] > b)
break; printf("%d\n", v[i]);
}
}
}
}

4190. Prime Palindromes 一亿以内的质数回文数的更多相关文章

  1. P1217 [USACO1.5]回文质数 Prime Palindromes(求100000000内的回文素数)

    P1217 [USACO1.5]回文质数 Prime Palindromes 题目描述 因为151既是一个质数又是一个回文数(从左到右和从右到左是看一样的),所以 151 是回文质数. 写一个程序来找 ...

  2. [LeetCode] Prime Palindrome 质数回文数

    Find the smallest prime palindrome greater than or equal to N. Recall that a number is prime if it's ...

  3. 洛谷 P1207 [USACO1.2]双重回文数 Dual Palindromes

    P1207 [USACO1.2]双重回文数 Dual Palindromes 题目描述 如果一个数从左往右读和从右往左读都是一样,那么这个数就叫做“回文数”.例如,12321就是一个回文数,而7777 ...

  4. 洛谷P1207 [USACO1.2]双重回文数 Dual Palindromes

    P1207 [USACO1.2]双重回文数 Dual Palindromes 291通过 462提交 题目提供者该用户不存在 标签USACO 难度普及- 提交  讨论  题解 最新讨论 暂时没有讨论 ...

  5. USACO Prime Palindromes 构造回文数

    这道题目一点也不卡素数的判断 就是朴素的sqrt(n) 也不卡 所以~放心的用吧. 构造回文的时候看了HINT 其中是这么写的: Generate palindromes by combining d ...

  6. URAL 2040 Palindromes and Super Abilities 2 (回文自动机)

    Palindromes and Super Abilities 2 题目链接: http://acm.hust.edu.cn/vjudge/contest/126823#problem/E Descr ...

  7. Queries for Number of Palindromes(求任意子列的回文数)

    H. Queries for Number of Palindromes time limit per test 5 seconds memory limit per test 256 megabyt ...

  8. luogu1207双重回文数[usaco1.2]Dual Palindromes

    题目描述 如果一个数从左往右读和从右往左读都是一样,那么这个数就叫做“回文数”.例如,12321就是一个回文数,而77778就不是.当然,回文数的首和尾都应是非零的,因此0220就不是回文数. 事实上 ...

  9. [Swift]LeetCode906. 超级回文数 | Super Palindromes

    Let's say a positive integer is a superpalindrome if it is a palindrome, and it is also the square o ...

随机推荐

  1. 算法笔记_057:蓝桥杯练习 最大的算式 (Java)

    目录 1 问题描述 2 解决方案   1 问题描述 问题描述 题目很简单,给出N个数字,不改变它们的相对位置,在中间加入K个乘号和N-K-1个加号,(括号随便加)使最终结果尽量大.因为乘号和加号一共就 ...

  2. 【Python】学习笔记十:字典

    字典是Python中唯一的映射类型(哈希表) 字典的对象时可变的,但字典的键值必须是用不可变对象,并且一个字典中可以使用不同类型的键值 1.定义字典 dict={key1:value1,key2:va ...

  3. c#导出到excel的几种解决方案

    (一)传统操作Excel遇到的问题: 1.如果是.NET[使用office组件Microsoft.Iffice.interop.Excel的话],需要在服务器端装Office,且及时更新它,以防漏洞, ...

  4. Mysql 中 HAVING 和 USING 的使用

    1. HAVING 在 SQL 中增加 HAVING 子句原因是,WHERE 关键字无法与合计函数一起使用. 实例:现表Orders(订单)有如下字段: Id,Date,Price ,Customer ...

  5. resin设置jvm参数

    http://www.quiee.com.cn/archives/592/ resin resin3.0 及前期版本内存设置, 如下: 启动时设置虚拟内存: unix> bin/httpd.sh ...

  6. MongoDB - 内嵌角色

    数据库用户角色(每个数据库都有的角色)   角色名 说明 read 对non-system集合的读取权限,以及以下system集合的读取权限:system.indexes,system.js,syst ...

  7. mongodb - save()和insert()的区别

    遇到_id相同的情况下:insert操作会报错:save完成保存操作 > db.person.find() > db.person.insert({"_id":1,ag ...

  8. python 配置

    一.下载 https://www.python.org/ftp/python/3.4.2/python-3.4.2.amd64.msi 二.配置python--eclipse插件 1.直接在eclip ...

  9. 简单记录一次ORA-00600 kcratr_nab_less_than_odr

    当前具体报错已经没有了,仅仅有对应图.參考EYGLE一篇文章中数据: 1.故障现象 数据库版本号11G,错误类似下面: ORA-00600: 内部错误代码, 參数: [kcratr_nab_less_ ...

  10. Python format 格式化函数 格式化字符串