回文素数


Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u

Description

xiaoou33对既是素数又是回文的数特别感兴趣。比如说151既是素数又是个回文。现在xiaoou333想要你帮助他找出某个范围内的素数回文数,请你写个程序找出 a 跟b 之间满足条件的数。(5 <= a < b <= 100,000,000); 
 

Input

这里有许多组数据,每组包括两组数据a跟b。
 

Output

对每一组数据,按从小到大输出a,b之间所有满足条件的素数回文数(包括a跟b)每组数据之后空一行。
 

Sample Input

       
       
5 500
 

Sample Output

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

#include <iostream>
#include <cmath> 
using namespace std;

int Prime(long int w) 
{ 
  int a, b,c; 
  a = w%2; 
  b=3; 
  c=(int)sqrt((double)w); 
  while (a&&b<=c) 
  { 
    a=w%b; 
    b+=2; 
  } 
  return a; 
} 

int main() 
{ 
  int a,b,c,d,e; 
  int m,n; 
  long int w; 
  while(cin >> m >> n)
  {
  a=0; 
  b=0; 
  c=0; 
  d=0; 
  e=4; 
       while (a<=9) 
       { 
         e++; 
         if (e==10) 
         { 
           e=0; 
           d++; 
         } 
         if (d==10) 
         { 
           d=0; 
           c++; 
         } 
         if (c==10) 
         { 
           c=0; 
           b++; 
         } 
         if (b==10) 
         { 
           b=0; 
           a++; 
         }
         w=a*100000000+b*10000000+c*1000000+d*100000+e*10000+d*1000+c*100+b*10+a; 
         while (w%10==0) 
         {
           w=w/10; 
         } 
         if( w >= m && w < n )
         { 
             if (Prime(w)) 
             { 
               cout << w << endl;
               if (w>5&&w<100) 
               { 
                 cout << 11 <<endl;
               } 
             } 
         } 
       }
       cout << endl;
  } 
return 0; 
}



第二代码,西西里上的题,输入0 0时结束程序

#include<iostream>
#include<cstdio>
#include<cmath>

using namespace std;
const int maxn=10000;

int a,b;
int ans[maxn];

bool ask(int x)
{
    for (int i=2; i<=sqrt(x); i++)
        if (x%i==0) return false;

    return true;
}
void prepare()
{
    for (int i=1; i<=maxn; i++){
        int state=i,tmp=i/10;
        while (tmp!=0){
            state=state*10+tmp%10;
            tmp/=10;
        }
        if (ask(state)) ans[++ans[0]]=state;
        if (state==7) ans[++ans[0]]=11;
    }
}
inline int init()
{
    while (scanf("%d%d",&a,&b)!=EOF){
          if(a==0&&b==0) return 0;
           for (int i=1; i<=ans[0]; i++)
                if ((ans[i]>=a) && (ans[i]<=b)) printf("%d\n",ans[i]);
    }
    return 0;
}
int main()
{
    prepare();
    init();
    return 0;
}


法3:

bool checkPri(int n)
{
	bool b=true;
	for(int i=2;i<=n/2;i++)
	{
		if(n%i==0)
		{
			b=false;break;
		}
	}
	return b;
}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值