867. Prime Palindrome

88 篇文章 0 订阅
42 篇文章 0 订阅

Find the smallest prime palindrome greater than or equal to N.

Recall that a number is prime if it's only divisors are 1 and itself, and it is greater than 1. 

For example, 2,3,5,7,11 and 13 are primes.

Recall that a number is a palindrome if it reads the same from left to right as it does from right to left. 

For example, 12321 is a palindrome.

 

Example 1:

Input: 6
Output: 7

Example 2:

Input: 8
Output: 11

Example 3:

Input: 13
Output: 101

 

Note:

  • 1 <= N <= 10^8
  • The answer is guaranteed to exist and be less than 2 * 10^8.

发现有个规律ABCCBA,ABCDDCBA这种偶数形式的一定不是质数(11除外),一定是11的倍数,所以只要找奇数位长度的数了

然后以为穷举+判断是不是prime即可,搜索的GeeksForGeeks的答案都是求出所有质数,然后循环,好无语,

自己真应该先试一下自己的穷举+判断是不是prime的想法的,乱七八糟的搜类似的题还真是浪费很多时间

class Solution:
    def primePalindrome(self, n):
        """
        :type N: int
        :rtype: int
        """
        def isPrime(t):
            if t==1: return False
            i = 2
            while i*i<=t:
                if t%i==0: return False
                i+=1
            return True
        
        if n<110:
            for i in range(n, 1000000):
                if str(i)==str(i)[::-1] and isPrime(i): return i
             
        s=str(n)
        if len(s)%2==0:
            p=len(s)//2
            t=int('1'+'0'*(p))
        else:
            p=len(s)//2
            t=int(s[:p+1])
            if int(s[:p+1][::-1])<int(s[p:]): t+=1
        
        while 1:  
            s = str(t)
            res = int(s+s[:-1][::-1])
            if isPrime(int(res)):
                return res
            else:
                t+=1

s=Solution()
#print(s.primePalindrome(1))
#print(s.primePalindrome(2))
#print(s.primePalindrome(6))
#print(s.primePalindrome(8))
#print(s.primePalindrome(13))
#print(s.primePalindrome(758))
#print(s.primePalindrome(9015110))
print(s.primePalindrome(85709140))

                
        
        

可以按照以下步骤来编写程序: 1. 定义函数 is_prime(n) 判断一个数 n 是否为素数,如果是素数返回 True,否则返回 False。 ```python def is_prime(n): if n < 2: # 小于2的数都不是素数 return False for i in range(2, int(n**0.5)+1): if n % i == 0: return False return True ``` 2. 定义函数 is_palindrome(n) 判断一个数 n 是否为回文数,如果是回文数返回 True,否则返回 False。 ```python def is_palindrome(n): s = str(n) return s == s[::-1] ``` 3. 编写主函数 main(n),根据输入的正整数 n 输出前 n 个回文素数,每行显示 5 个并适当对齐。 ```python def main(n): count = 0 # 记录输出的个数 num = 2 # 从2开始找 while count < n: if is_prime(num) and is_palindrome(num): print('{:>8d}'.format(num), end='') # 格式化输出并对齐 count += 1 if count % 5 == 0: print() # 每输出5个换行 num += 1 ``` 4. 最后在主程序中调用 main(n) 函数即可。 完整代码如下: ```python def is_prime(n): if n < 2: # 小于2的数都不是素数 return False for i in range(2, int(n**0.5)+1): if n % i == 0: return False return True def is_palindrome(n): s = str(n) return s == s[::-1] def main(n): count = 0 # 记录输出的个数 num = 2 # 从2开始找 while count < n: if is_prime(num) and is_palindrome(num): print('{:>8d}'.format(num), end='') # 格式化输出并对齐 count += 1 if count % 5 == 0: print() # 每输出5个换行 num += 1 if __name__ == '__main__': n = int(input('请输入一个正整数 n:')) main(n) ``` 输入一个正整数 n,程序将会输出前 n 个回文素数,每行显示 5 个,并适当对齐。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值