Prime Palindrome Golf

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

时间: 2024-08-07 00:09:15

Prime Palindrome Golf的相关文章

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

P1217 [USACO1.5]回文质数 Prime Palindromes 题意:给定一个区间,输出其中的回文质数: 学习了洛谷大佬的回溯写法,感觉自己写回溯的能力不是很强: #include <cstdio> #include <iostream> #include <cmath> const int maxn = 100; using namespace std; int a[maxn],l,r; bool is_prime(int n) //判断素数 { int

hoj 1004 Prime Palindromes(还是不够完美)

The number 151 is a prime palindrome because it is both a prime number and a palindrome (it is the same number when read forward as backward). Write a program that finds all prime palindromes in the range of two supplied numbers a and b (5 <= a < b

USACO 1.5 Prime Palindromes

Prime Palindromes The number 151 is a prime palindrome because it is both a prime number and a palindrome (it is the same number when read forward as backward). Write a program that finds all prime palindromes in the range of two supplied numbers a a

【LeetCode】数学(共106题)

p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica } [2]Add Two Numbers [7]Reverse Integer [8]String to Integer (atoi) [9]Palindrome Number [12]Integer to Roman [13]Roman to Integer [29]Divide Two Integers [43]Multiply Strings [50]Pow(x,

luogu P1217 [USACO1.5]回文质数 Prime Palindromes x

P1217 [USACO1.5]回文质数 Prime Palindromes 题目描述 因为151既是一个质数又是一个回文数(从左到右和从右到左是看一样的),所以 151 是回文质数. 写一个程序来找出范围[a,b](5 <= a < b <= 100,000,000)( 一亿)间的所有回文质数; 输入输出格式 输入格式: 第 1 行: 二个整数 a 和 b . 输出格式: 输出一个回文质数的列表,一行一个. 输入输出样例 输入样例#1: 5 500 输出样例#1: 5 7 11 101

USACO Prime Palindromes 构造回文数

这道题目一点也不卡素数的判断 就是朴素的sqrt(n) 也不卡 所以~放心的用吧. 构造回文的时候看了HINT 其中是这么写的: Generate palindromes by combining digits properly. You might need more than one of the loops like below. /* generate five digit palindrome: */ for (d1 = 1; d1 <= 9; d1+=2) { /* only odd

codechef Prime Palindromes 题解

给定一个数,求一个新数要大于等于这个数,而这个新数既要是palindromes回文又要是prime素数. 题目很简单,有人都使用取巧的方法保存好结果直接查表. 或者暴力法求解. 这里不使用保存表的方法,也不要用暴力法.- 这些方法都不好. 使用的技巧有: 1 而是使用next palindrome的技巧,只需要O(n),n是数位,可以认为是常数了. 2 判断素数的方法,时间效率是O(sqrt(n)), n是数值大小,如果是重复判断很多数是否是素数是有办法优化的,但是如果是单个素数判断的话,我还想

P1217【洛谷FromUSACO】[USACO1.5]回文质数 Prime Palindromes

直接上题——[传送门:http://www.luogu.org/problem/show?pid=1217] 题目来源USACO,NOCOW翻译,洛谷转载,懂(F_Q)的童鞋们可以去USACO官网逛逛……<!———————————下面是原题—————————————————————————————--> P1217 [USACO1.5]回文质数 Prime Palindromes 题目描述 因为151既是一个质数又是一个回文数(从左到右和从右到左是看一样的),所以 151 是回文质数.写一个程

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

P1217 [USACO1.5]回文质数 Prime Palindromes 题目描述 因为151既是一个质数又是一个回文数(从左到右和从右到左是看一样的),所以 151 是回文质数. 写一个程序来找出范围[a,b](5 <= a < b <= 100,000,000)( 一亿)间的所有回文质数; 输入输出格式 输入格式: 第 1 行: 二个整数 a 和 b . 输出格式: 输出一个回文质数的列表,一行一个. 输入输出样例 输入样例#1: 复制 5 500 输出样例#1: 复制 5 7