Prime and palindrome

本文介绍了一个算法挑战,即找到大于等于给定整数N的最小回文质数。文章详细解释了如何判断一个数是否为回文数及质数,并通过Python代码实现了这一过程。了解这些内容对于从事加密技术、数学软件开发等领域非常有用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

     An integer is said to be a palindrome if it is equal to its reverse in a string form. For example, 79197 and 324423 are palindromes. In this task you will be given an integer N. You must find the smallest integerM >= N such that M is a prime number and also apalindrome

Input: N. An integer.

Output: A prime palindrome. An integer.

Example:

?
1
2
3
checkio(31)==101
checkio(130)==131
checkio(131)==131

How it is used: Prime numbers are very useful for many IT sectors (cryptography, mathematical software). 



__author__ = 'Administrator'
import math
def isPalindrome(item):
    s=str(item)
    rs=''
    for c in reversed(s):
        rs+=c
    if s==rs:
        return True
    else:
        return False
def isPrime(item):
    if item==1:
        return False
    i=2
    m=math.sqrt(item)
    while i<=m:

        if item%i==0:
            return False
        i+=1
    return True


def isPalindrom2(n):
    """
    Check integer for palindrom
    >>> isPalindrom(131)
    True
    >>> isPalindrom(130)
    False
    """
    return str(n) == str(n)[::-1]
def checkio(item):
    while True:
        if isPalindrome(item) and isPrime(item):
            return item
        else:
            item+=1

print(checkio(2))


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值