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;
Prime Palindromes( 素数回文) C++实现
最新推荐文章于 2024-10-01 20:33:59 发布