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 symmetrical. 
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 is a code golf mission and 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.

题目大义: 找出比输入数字大的, 且是回文数及素数的数字; 要求代码尽可能短;

如: golf(2) = 3, golf(13) == 101, golf(101) = 131

 def T(n):
for i in range(2, n):
if n % i == 0:
return False return True def golf(number):
for i in range(number + 1, 98690):
if i == int(str(i)[::-1]) and T(i):
return i

第一份代码中规中矩, 回文数的判断使用str的切片[::-1], 至于98690的限制, 由于题目中的number >= 0 and number < 98689, 而98689正好是回文数且又是素数, 因此结果的最大可能为98689

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

第二份代码省去了使用函数判断素数, 使用列表解析判断素数, 在区间[2, x), 如果没有能够整除的数, 即为素数

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

第三份代码, 使用all判断是否均为不可整除的数

 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)]))

第四份代码, 使用了generator将for循环融合, 调用next获得下一个元素, 即结果; 并使用了lambda表达式

Prime Palindrome Golf的更多相关文章

  1. [LeetCode] Prime Palindrome 质数回文数

    Find the smallest prime palindrome greater than or equal to N. Recall that a number is prime if it's ...

  2. [Swift]LeetCode866. 回文素数 | Prime Palindrome

    Find the smallest prime palindrome greater than or equal to N. Recall that a number is prime if it's ...

  3. LeetCode 866. Prime Palindrome

    866. Prime Palindrome(回文素数) 题目: 求出大于或等于 N 的最小回文素数. 回顾一下,如果一个数大于 1,且其因数只有 1 和它自身,那么这个数是素数. 例如,2,3,5,7 ...

  4. 866. Prime Palindrome

    Find the smallest prime palindrome greater than or equal to N. Recall that a number is prime if it's ...

  5. 洛谷 P1217 [USACO1.5]回文质数 Prime Palindrome

    嗯... 这道题对于蒟蒻的我来说实在是TQL... 先看一下题:(题目链接:https://www.luogu.org/problemnew/show/P1217) 然后说一下我的做题过程吧: 一看到 ...

  6. 洛谷P1217回文质数-Prime Palindrome回溯

    P1217 [USACO1.5]回文质数 Prime Palindromes 题意:给定一个区间,输出其中的回文质数: 学习了洛谷大佬的回溯写法,感觉自己写回溯的能力不是很强: #include &l ...

  7. USACO 1.5 Prime Palindromes

    Prime Palindromes The number 151 is a prime palindrome because it is both a prime number and a palin ...

  8. 4190. Prime Palindromes 一亿以内的质数回文数

    Description The number 151 is a prime palindrome because it is both a prime number and a palindrome ...

  9. <Sicily>Prime Palindromes

    一.题目描述 The number 151 is a prime palindrome because it is both a prime number and a palindrome (it i ...

随机推荐

  1. 【绿茶书情】:《SOHO小报》和《凤… - 绿茶的日志 - 网易博客

    [绿茶书情]:<SOHO小报>和<凤- - 绿茶的日志 - 网易博客 [绿茶书情]:<SOHO小报>和<凤-  

  2. Oracle百问百答(四)

    Oracle百问百答(四) 31.怎样查看某用户下的表? select table_name from all_tables where owner=upper('jhemr'); 32.怎样查看某用 ...

  3. poj 3744 Scout YYF I (矩阵)

    Description YYF -p. Here is the task, given the place of each mine, please calculate the probality t ...

  4. python3-day3(函数-返回值)

    1.函数 函数式:将某功能代码封装到函数中,日后便无需重复编写,仅调用函数即可 面向对象:对函数进行分类和封装,让开发“更快更好更强...” 2.return返回值 import smtplibfro ...

  5. (转)iOS7界面设计规范(7) - UI基础 - 交互性与反馈

    现在只是周日下午,可怎样都觉得整个周末就这样即将过去了,不免沮丧.看了好多集小丸子了,又不免觉得现在其实是在放暑假,可以一天一天的窝在家里做任何事,任何事.再上一篇iOS7设计规范,然后本周末就到这里 ...

  6. Unity 对象池的使用

    在游戏开发过程中,我们经常会遇到游戏发布后,测试时玩着玩着明显的感觉到有卡顿现象.出现这种现象的有两个原因:一是游戏优化的不够好或者游戏逻辑本身设计的就有问题,二是手机硬件不行.好吧,对于作为程序员的 ...

  7. docke 网络配置2

    一,docker 的bridge模式是和vmware中的nat模式类似的,但是如果想要弄成和vmwae中的bridge怎么办呢? 说明,bridge模式获取的Ip是与宿主机的ip是出于同一个网段的. ...

  8. VS2015 添加DNX SDK

    第一次运行VS2015,添加第一个ASP.NET 5程序时会报一个错误“DNX SDK版本 “dnx-clr-win-x86.1.0.0-beta5”无法安装. 解决办法: 打开CMD :输入 @po ...

  9. Html与css基础

    1.html的定义 (1).html:超文本标记语言(HyperText Markup Language),它主要包括"头"(Head)和"主体"(Body)两 ...

  10. Android开发_关于中英文切换

    开发APP过程中可能要有中文模式和英文模式,切换后控件要随着进行更改,以下代码可以很好的实现: public static int getResourcesId(Context context, St ...