Prime palindrome golf

Source: Internet
Author: User
Prime palindrome golf

Do you know how to play prime palindrome golf? You are given a number and your challenge is to find the closest palindromic prime number that greater than what you were given.

A palindromic number or numeral palindrome is a number that remains the same when its digits are reversed, such as 79197. These numbers appear to be called rical.
A prime number is a natural number greater than 1 that has no positive divisors other than 1 and itself. In this task you will be given an positive integer. You should find the closest integer that:
-Is greater than the given number;
-Is prime;
-Is palindromic.
For example: for the number 13, the closest greater palindromic Prime is 101. Or, for the number is 2, the answer is 3, because any one-digit number is a palindrome.

We have one more rule for this challenge. This isCode golf missionAnd your main goal is to make your code as short as possible. the shorter your code, the more points you earn. your score for this mission is dynamic and directly related to the length of your code. for reference, scoring is based on the number of characters used. 250 characters is the maximum allowable and it will earn you zero points. for each character less than 250, you earn 1 point. for example for 200 character long code earns you 50 points.

Important:We are using default recursion limit (100). So don't try to solve this mission with recursion.

Input:A number as an integer.

Output:The closest greater palindromic prime as an integer.

Question: Find a number that is larger than the input number and is the number of the input number and prime number; the Code must be as short as possible;

For example, golf (2) = 3,golf(13) == 101, Golf (101) = 131

 1 def T(n): 2     for i in range(2, n): 3         if n % i == 0: 4             return False 5  6     return True 7  8 def golf(number): 9     for i in range(number + 1, 98690):10         if i == int(str(i)[::-1]) and T(i):11             return i

In the first code, the part [:-1] of STR is used to judge the number of input files. As for the limit of 98690, the number in the question is greater than or equal to 0 and number <98689, and 98689 is exactly the number of input and the prime number, so the maximum result may be 98689

1 def golf(n):2     for x in range(n + 1, 98690):3         if str(x)[::-1] == str(x) and [x % i for i in range(2, x)].count(0) == 0:4             return x

The second Code removes the need to use a function to determine the prime number and use list parsing to determine the prime number. In the range [2, x), if there is no divisible number, it is the prime number.

1 def golf(n):2     for x in range(n + 1, 98690):3         if str(x)[::-1] == str(x) and all([x % i != 0 for i in range(2, x)]):4             return x

The third code uses all to determine whether all values are non-divisible.

1 golf = lambda n: next(x for x in range(n + 1, 98690) if str(x)[::-1] == str(x) and all([x % i != 0 for i in range(2, x)]))

The fourth Code uses generator to combine the for loop, call next to obtain the next element, that is, the result, and use the lambda expression.

 

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.