Prime Palindromes( 素数回文) C++实现

 Prime Palindromes

Time limit:     15sec.     Submitted:     10679
Memory limit:     32M     Accepted:     1958
Source: USACO Gateway

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 <= 1000,000,000); both a and b are considered to be within the range .
Input

Line 1: Two integers, a and b
Output

The list of palindromic primes in numerical order, one per line.
Sample Input

5 500

Sample Output

5
7
11
101
131
151
181
191
313
353
373
383



开始拿到题目的时候,一看。恩!挺简单的,于是边哗啦啦地开始coding。
很快,代码写好了。
首先我们先判断以下一个数是不是素数,这个就暂时想到朴素素数法了。
bool is_primer(const unsigned int number)
 28 {
 29     int i;
 30     unsigned int sqrt_num;
 31     sqrt_num = sqrt(number + 1);
 32     for(i = 3; i <= sqrt_num; i += 2)
 33     {
 34         if((number % i) == 0)
 35           return false;
 36     }
 37     return true;
 38 }
接下来就是该写palindromele了, 自己也没有多考虑,就是按正常的方法直接把函数写完,如下所示:
bool is_palindrome(const unsigned int number)
 41 {
 42     int button , top, middle;
 43     unsigned int temp = number;
 44     button = top = 0;
 45     int i = 0;

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值