Luogu--Prime Palindromes

Title description
Because 151 is both a prime number and a palindrome number (from left to right and right to left are the same), 151 is a palindrome prime number.
Write a program to find all palindrome prime numbers in the range [a,b] (5≤a<b≤100,000,000) (100 million).
Input format
Line 1: Two integers a and b.
Output format
Output a list of palindrome prime numbers, one per line.
Sample input and output

Enter #1 Output #1
5 500 5
7
11
101
131
151
181
191
313
353
373
383

Idea: The input interval may be very large. According to the conventional enumeration, T will be dropped. Therefore, this question directly constructs a palindrome string, and then judges whether it is a prime number (the mantissa of the number must not be an odd number, and the mantissa is determined The loop += 2 can reduce some unnecessary loops), then you can ac this question.

code show as below:

#include <iostream>
#include <cmath>
using namespace std;
int judge(int x)
{
    
    
 int i;
 if(x==1)
 return 0;
 for(i=2;i<=sqrt(x);i++)
 {
    
    
  if(x%i==0)
  return 0;
 }
 return 1;
}
int main()
{
    
    
 int d1,d2,d3,d4,d5,shu,l,r;
 cin>>l>>r;
 if(l<=10)
 for(d1=1;d1<=9;d1++)
 {
    
    
  shu=d1;
  if(shu>r)
  break;
  if(shu<l)
  continue;
  if(judge(shu))
  cout<<shu<<endl;
 }
 if(l<=100)
 for(d1=1;d1<=9;d1+=2)
 {
    
    
  shu=d1*10+d1;
  if(shu>r)
  break;
  if(shu<l)
  continue;
  if(judge(shu))
  cout<<shu<<endl;
 }
 if(l<=1000)
 for(d1=1;d1<=9;d1+=2)
 {
    
    
  for(d2=0;d2<=9;d2++)
  {
    
    
   shu=d1*100+d2*10+d1;
   if(shu>r)
   break;
   if(shu<l)
   continue;
   if(judge(shu))
   cout<<shu<<endl;
  }
 }
 if(l<=10000)
 for(d1=1;d1<=9;d1+=2)
 {
    
    
  for(d2=0;d2<=9;d2++)
  {
    
    
   shu=d1*1000+d2*100+d2*10+d1;
   if(shu>r)
   break;
   if(shu<l)
   continue;
   if(judge(shu))
   cout<<shu<<endl;
  }
 }
 if(l<=100000)
 for(d1=1;d1<=9;d1+=2)
 {
    
    
  for(d2=0;d2<=9;d2++)
  {
    
    
   for(d3=0;d3<=9;d3++)
   {
    
    
    shu=d1*10000+d2*1000+d3*100+d2*10+d1;
    if(shu>r)
    break;
    if(shu<l)
    continue;
    if(judge(shu))
    cout<<shu<<endl;
   }
  }
 }
 if(l<=1000000)
 for(d1=1;d1<=9;d1+=2)
 {
    
    
  for(d2=0;d2<=9;d2++)
  {
    
    
   for(d3=0;d3<=9;d3++)
   {
    
    
    shu=d1*100000+d2*10000+d3*1000+d3*100+d2*10+d1;
    if(shu>r)
    break;
    if(shu<l)
    continue;
    if(judge(shu))
    cout<<shu<<endl;
   }
  }
 }
 if(l<=10000000)
 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++)
    {
    
    
    shu=d1*1000000+d2*100000+d3*10000+d4*1000+d3*100+d2*10+d1;
    if(shu>r)
    break;
    if(shu<l)
    continue;
    if(judge(shu))
    cout<<shu<<endl;
    }
   }
  }
 }
 if(l<=100000000)
 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++)
    {
    
    
    shu=d1*10000000+d2*1000000+d3*100000+d4*10000+d4*1000+d3*100+d2*10+d1;
    if(shu>r)
    break;
    if(shu<l)
    continue;
    if(judge(shu))
    cout<<shu<<endl;
    }
   }
  }
 }
 return 0;
}

Guess you like

Origin blog.csdn.net/HT24k/article/details/109238645