P1217 "Luo gu fromusaco" [USACO1.5] palindrome prime number Prime palindromes

Source: Internet
Author: User

Directly on the question--
"Portal: http://www.luogu.org/problem/show?pid=1217"

Source Usaco,nocow translation, Luo Valley reprint, Understand (F_Q) children's shoes can go to Usaco official website to stroll ...
<!——————————— here is the original question —————————————————————————————-->

P1217 [USACO1.5] palindrome prime number Prime palindromes

Title Description
Since 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 format: line 1th: Two integers a and b.

Output format: Output A list of palindrome prime numbers, one line at a.

Input/Output sample
Input Sample # #:
5 500
Sample # # of output:
5
7
11
101
131
151
181
191
313
353
373
383

---------------------------------------------------------------------------
Description
Hint 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:

 for(D1 =1; D1 <=9; d1+=2) {//only odd numbers are primes .      for(D2 =0; D2 <=9; d2++) {          for(D3 =0; D3 <=9; d3++) {palindrome=10000*d1 + +*D2 + -*d3 +Ten*d2 + D1;//(processing palindrome number ...)         }     } }


<!——————————————— problem solving process ———--—————————————————————-->
Just at the beginning was the last to provide a method to generate palindrome number of chaos, the result of a seventy or eighty-line program, logic is particularly chaotic, and finally found that the first one to produce prime numbers more convenient

//This procedure is not wrong, but the last Test point timed out, only took 90 minutes program accepted;varA,b,i,ans:longint;//---------------------------------------------------functionCheck (k:longint): boolean;//Judging prime numbers and whether Palindromevarlen:longint; st:string;beginstr (K,ST);//goto string len:=Length (ST); fori:=1  to(len+1)Div 2  Do    ifst[i]<>st[len+1-I.] Thenexit (FALSE);//Judge Palindrome fori:=2  toTrunc (sqrt (k)) Do   ifKMoDI=0  Thenexit (FALSE);//Determine the prime number exit (true);End;//---------------------------------------------------beginReadln (A, B);//Read into data ans:=a;//Enumeration starting from a whileAns<=b Do    begin      ifCheck (ANS) Thenwriteln (ANS);//Qualified OutputifOdd (ans) Thenans:=ans+2//is an odd increment of 2        ElseInc (ANS);//is an even increment of 1End;End.

On this machine to try to run a bit, according to the conditions of the problem, to find all the palindrome prime number within 100 million, the results found that there is no palindrome prime number between 10 million to 100 million, then add multiple if statements can save a lot of time to find the number of palindrome after 10 million

The Customs code is as follows----------------------------------------------------------------------------

//Tsutenkaku Tower ~program accepted;varA,b,i,ans:longint;//---------------------------------------------------functionCheck (k:longint): boolean;//Judging prime numbers and whether Palindromevarlen:longint; st:string;beginstr (k,st);//go to string len:=length (ST);//Request Length fori:=1  to(len+1)Div 2  Do    ifst[i]<>st[len+1-I.] ThenExit (false);//Judging palindrome fori:=2  toTrunc (sqrt (k)) Do   ifKMoDI=0  ThenExit (false);//determine the prime number exit (true);End;//---------------------------------------------------beginReadln (A, b);//read in data ans:=a;//enumeration starting from a whileAns<=b Do    begin      ifAns>10000000  Thenbreak;//more than 10 million no longer looking forifCheck (ANS) ThenWriteln (ANS);//Eligible outputifOdd (ans) Thenans:=ans+2//is an odd increment of 2ElseInc (ANS);//is an even increment of 1End;End.


-----------------------------------------------------------------------------------------

Blog of the author of this article garden: http://www.cnblogs.com/tonylim/

CSDN Blog of the author of this article: http://blog.csdn.net/happy_china1312

P1217 "Luo gu fromusaco" [USACO1.5] palindrome prime number Prime palindromes

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.