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. c语言条件表达式误区1

    #include <stdio.h> #include <stdlib.h> //综合1 和 2我们知道牢记条件表达式中常量写在左边的语法规则 以防因为疏忽造成难以查找的错误 ...

  2. Firefox历史版本下载

    http://ftp.mozilla.org/pub/firefox/releases/ http://ftp.mozilla.org/pub/firefox/releases/47.0.1/

  3. 设计: ListView 接口,and the missing read-only interfaces in java collection framework

    Java的集合框架以其成功易用的设计征服了很多人(包括我),并且教科书式的诠释了泛型的应用方式. 我也是被 Joshua Bloch 的书引领入门,从中得益良多.我当然不会认为自己在设计上比他懂得更多 ...

  4. HashMap的存储结构及原理

    1.HashMap的数据结构(HashMap通过hashcode对其内容进行高速查找,是无序的) 数据结构中有数组和链表来实现对数据的存储,但这两者基本上是两个极端. 数组 :数组的存储区是连续的,占 ...

  5. [Redux] Redux: Extracting Container Components -- AddTodo

    Code to be refactored: const AddTodo = ({ onAddClick }) => { let input; return ( <div> < ...

  6. javascript什么是函数

    函数是完成某个特定功能的一组词语.如没有函数,完成任务可能需要五行.十行.甚至更多的代码. 这是未满就可以把完成特定功能的代码块放到一个函数里,直接调用这个函数,就省重复输入大量代码的麻烦. 如何定义 ...

  7. 多重和嵌套if

    多重if实例: 看例子,内容不解释了! 隐藏行号 复制代码 ? 多重if import java.util.Scanner; public class 多重if{ public static void ...

  8. 浅谈Block传值-匿名函数(代码块)

    block传值是从后往前传值---代理也是 // 使用block时, 不能访问self, 也不能直接访问属性, self.属性, 用self调用方法; 只要这样做了, block都会对其强引用一份, ...

  9. PHP上传文件DEMO

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Frameset//EN"> <html> <head> ...

  10. (一)backbone - API入门

    初探 backbone采用MVC模式,本身提供了模型.控制器和视图从而我们应用程序的骨架便形成. Backbone.js 唯一重度依赖 Underscore.js. 对于 RESTful , hist ...