码迷,mamicode.com
首页 > 其他好文 > 详细

Prime Palindrome Golf

时间:2014-08-05 13:24:49      阅读:274      评论:0      收藏:0      [点我收藏+]

标签:style   blog   color   使用   os   io   strong   for   

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

 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

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

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

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

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

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

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

 

Prime Palindrome Golf,布布扣,bubuko.com

Prime Palindrome Golf

标签:style   blog   color   使用   os   io   strong   for   

原文地址:http://www.cnblogs.com/hzhesi/p/3891711.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!