Prime Palindromes


Prime Palindromes

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 .

PROGRAM NAME: pprime

INPUT FORMAT

Line 1:Two integers, a and b

SAMPLE INPUT (file pprime.in)

5 500

OUTPUT FORMAT

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

SAMPLE OUTPUT (file pprime.out)

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

HINTS (use them carefully!)

做这题学了好多东西:1.题目提供了思路***生成1..1e8范围内的回文数,然后判断它是否是素数。

                 2.任意偶数长度的回文数都不可能为质数(除了11),因为它能被11整除,而11恰好只有自身和1两个因子。除2外,所有偶数均不可能是质数。

                 3.判断质数的时候有一个有效的优化,就是用筛法筛出1..10000(一万)以内的质数,再转存到数组q中,然后判断时只要用待判断的数mod q[n] 就可以了.


/*
ID: des_jas1
PROG: pprime
LANG: C++
*/
#include <iostream>
#include <fstream>
#include <string.h>
#include <cmath>
#include <algorithm>
//#define fin cin
//#define fout cout
using namespace std;

const int MAX=10001,b[11]={0,1,10,100,1000,10000,100000,1000000,10000000,100000000,1000000000};
int num=0,pr[MAX],qt=1,a,bb;
bool prime[MAX];

void initial()
{
	int i,j;
	memset(prime,0,sizeof(prime));
	pr[0]=2;
	prime[2]=true;
	for(i=3;i<MAX;i+=2)
		prime[i]=true;
	for(i=3;i<MAX;i+=2)
	{
		if(prime[i])
		{
			pr[qt++]=i;
			for(j=i*3;j<MAX;j+=i*2)
			{
				prime[j]=false;
			}
		}
	}
}

void IsPrime(int t,ofstream& fout)
{
	int i,tp;
	tp=int(sqrt(t));
	for(i=0;pr[i]<=tp;i++)
	{
		if(!(t%pr[i]))
			return;
	}
	fout<<t<<endl;
	
}

void generate(int aa,ofstream& fout)
{
	int d1,d2,d3,d4,d5;
    int palindrome;
	switch(aa)
	{
	case 1:for (d1=5;d1<=9;d1+=2)
			{
			    palindrome=d1;
				if(palindrome<a)
					continue;
				if(palindrome>bb)
					break;
				IsPrime(palindrome,fout);
			}
		break;
	case 2:for (d1=1;d1<=9;d1+=2)
			{
				palindrome=b[2]*d1+d1;
				if(palindrome<a)
					continue;
				if(palindrome>bb)
					break;
				IsPrime(palindrome,fout);
			}
		break;
	case 3:for (d1=1;d1<=9;d1+=2)
		for (d2=0;d2<=9;d2++)
			{
	            palindrome=b[3]*d1+b[2]*d2+d1;
				if(palindrome<a)
					continue;
				if(palindrome>bb)
					break;
				IsPrime(palindrome,fout);
			}
		break;
	case 5:for (d1=1;d1<=9;d1+=2)
		for (d2=0;d2<=9;d2++)
			for (d3=0;d3<=9;d3++)
			{
	            palindrome=b[5]*d1+b[4]*d2+b[3]*d3+b[2]*d2+d1;
				if(palindrome<a)
					continue;
				if(palindrome>bb)
					break;
				IsPrime(palindrome,fout);
			}
			break;
	case 7:for (d1=1;d1<=9;d1+=2)
		for (d2=0;d2<=9;d2++)
			for (d3=0;d3<=9;d3++)
				for (d4=0;d4<=9;d4++)
			{
	            palindrome=b[7]*d1+b[6]*d2+b[5]*d3+b[4]*d4+b[3]*d3+b[2]*d2+d1;
				if(palindrome<a)
					continue;
				if(palindrome>bb)
					break;
				IsPrime(palindrome,fout);
			}
			break;
	case 9:for (d1=1;d1<=9;d1+=2)
		for (d2=0;d2<=9;d2++)
			for (d3=0;d3<=9;d3++)
				for (d4=0;d4<=9;d4++)
					for (d5=0;d5<=9;d5++)
			{
	            palindrome=b[9]*d1+b[8]*d2+b[7]*d3+b[6]*d4+b[5]*d5+b[4]*d4+b[3]*d3+b[2]*d2+d1;
				if(palindrome<a)
					continue;
				if(palindrome>bb)
					break;
				IsPrime(palindrome,fout);
			}
			break;
	
	default:break;
	}
 
}

int main() 
{
	ofstream fout ("pprime.out");
    ifstream fin ("pprime.in");
	int low,top,d=9;
	fin>>a>>bb;
	initial();
	for(low=1;a>=b[low];low++);
    for(top=1;bb>=b[top];top++);
	low--;
	top--;
	for(;low<=top;low++)
		generate(low,fout);
	fout.close();
	fin.close();
    return 0;
}




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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值