回文素数Python代码
时间: 2024-06-24 19:00:53 浏览: 9
回文素数是指正序和反序读都一样的素数。在Python中,我们可以编写一个函数来检查一个数是否既是回文又是素数。这里是一个简单的示例:
```python
def is_palindrome(n):
return str(n) == str(n)[::-1]
def is_prime(n):
if n <= 1:
return False
for i in range(2, int(n**0.5) + 1):
if n % i == 0:
return False
return True
def is_palindrome_prime(num):
if is_palindrome(num) and is_prime(num):
return True
else:
return False
# 测试
num = 121
if is_palindrome_prime(num):
print(f"{num} 是回文素数")
else:
print(f"{num} 不是回文素数")
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![py](https://img-home.csdnimg.cn/images/20210720083646.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)