Rokua-Palindrome prime numbers-process functions and recursion

Source: Internet
Author: User

Title DescriptionDescription because 151 is both a prime and a palindrome number (from left to right and from right to left to see the same), so 151 is a palindrome prime.
Write a program to find all palindrome prime numbers between range [A, b] (5 <= a < <= 100,000,000) (100 million); input/output format input/output Input Format:
Line 1th: Two integers a and b.
output Format:
Output a list of palindrome prime numbers, one line at a. input and Output sample sample Input/output sample Test point # # Input Sample:5 500 Sample output:

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

DescriptionDescriptionhint 1:generate the palindromes and see if they is prime.
Tip 1: Find out all the palindrome numbers and then determine if they are prime numbers (primes).

Hint 2:generate palindromes by combining digits properly. You might need more than one of the loops like below.
Tip 2: To produce the correct palindrome number, you may need several loops like the one below.

The title translation comes from Nocow.
Usaco Training Section 1.5

Generate a palindrome number with a length of 5:
1  for(D1 =1; D1 <=9; d1+=2) {//only odd numbers are primes .2       for(D2 =0; D2 <=9; d2++) {3           for(D3 =0; D3 <=9; d3++) {4Palindrome =10000*d1 + +*D2 + -*d3 +Ten*d2 + D1;//(processing palindrome number ...)5          }6      }7}

Idea: This difficulty is a bit big, the scope unexpectedly has 100 million, too pit daddy! So I made a few simplifications to it:

① from the odd start, each time i=i+2;

② no even number of palindrome prime numbers, save a lot of time, it is really beneficial to mankind ah!

③ exclude multiples of 2, 5

Each function implementation process is detailed:

① Judging palindrome Number: Deposit An array to determine whether the previous sweep and backward sweep are consistent.

② determine the number of digits (whether the detection length is greater than 100 million): From the previous sweep, back to the number of cycles.

③ Judge Prime number (this is very important OH): I start from 3, each i=i+2, and then use that number to try to remove I, if just do, then is prime, return 1.

The code is as follows:

1#include <stdio.h>2#include <string.h>3 intHuiwen (intk);//Judging palindrome number4 intHwlength (intk);//calculate the length of a palindrome number5 intPrimeintk);//Judging Prime numbers6 intMain ()7 {8     inta,b,i,j;9scanf"%d%d",&a,&b);Ten      for(i=a;i<=b;i++) One     { A         if(i%2==0&&i!=2)//exclude multiples of 2 -             Continue; -         if(i%5==0&&i!=5)//exclude multiples of 5 the             Continue; -         if(Hwlength (i)%2==0&&i!= One)//the length of the palindrome number is even and not one -             Continue; -         if(Huiwen (i)! =1)//not a palindrome, just skip. +             Continue; -         if(Prime (i))//is prime +printf"%d\n", i); A     } at     return 0; - } - intHuiwen (intk) - { -     inta[Ten],i=0, J; -      while(k>0)//each bit is stored in an array and then judged in     { -a[i]=k%Ten; tok=k/Ten; +i++; -     } the      for(j=0; j<i;j++)//Double cycle judgment (front and rear sweep method) *         if(a[j]!=a[i-j-1])//No, return 0 $             return 0; Panax Notoginseng     return 1;//after the end, yes, return 1 - } the intHwlength (intk) + { A     inta[Ten],i=0; the      while(k>0) +     { -a[i]=k%Ten; $K/=Ten; $i++; -     } -     return(i);//I was cycled. the } - intPrimeintk)Wuyi { the     inti; -      for(i=3; i*i<=k;i=i+2)//This can reduce multiple cycles. Wu         if(k%i==0) -         { About             return 0; $         }            -     return 1; -}

Rokua-Palindrome prime numbers-process functions and recursion

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.