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 <= 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

代码:

#include<stdio.h>

#include<stdlib.h>

#include<math.h>

/*

目的:输入两个数 输出这两个数之间的既是回文数又是素数的数*/

int prime(int n)/*判断一个数是不是素数*/

{

    int k=0,m,flag=1;/*定义变量名*/

    m=(int)sqrt(n);

    for (k=2;k<=m&&flag;k++)/*通过循环判断一个数有没有因数*/

    {

        if (n%k==0)

            flag=0;

    }

    if (flag)

        return 1;

    else

        return 0;

}

int  palindrome(long n)/*判断一个数是不是回文数*/

{

    long i=0,m=n;

    while (m!=0)/*把数字从头到尾反过来然后与原数比较 若想等则输出是回文数*/

    {

        i=i*10+m%10;

        m=m/10;

    }

    if (i==n)

        return n;

    else

        return 0;

}

 

int main()

{

    int i,a,b;

    char c;

    do

    {

        printf("please input two numbers,the frist must be litter than the second./n");

        printf("And the two numbers must be litter than 1000000000./n");

        scanf(" %d%*c%d",&a,&b);

        if (a>=b||b>1000000000)

        {

        printf("what you enter is wrong,please correct them./n");

        }

        else

        {

 

            for (i = a; i < b; i++)/*从输入的数开始算起到最后一个数挨个判断是不是回文数和素数*/

             {

              if ( palindrome(i)&& prime(i))

              {

                printf("%d/n", i);

              }

             }

            }

        printf("If you want to end the game please enter 0,else please enter anyother character./n");

        scanf(" %c",&c);

    }

    while (c!='0');/*循环进行上述程序直至用户输入0停止循环*/

    printf("The game is end");

 

    return 0;

}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值