回文质数回文质数是质数,也是回文质数。例如,131是质数,也是回文质数,313和757也是。编写一个程序,显示前100个回文质数。每行显示10个数字,中间间隔一个空格,如下所示:
时间: 2024-10-25 17:15:13 浏览: 11
回文质数是指同时具备质数和回文这两个特性的正整数。在编程中,要找出回文质数并显示前100个,通常会涉及到质数判断和字符串反转的双重检查。这里提供一个简单的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
count = 0
i = 101 # Start from 101 since 101 is the first palindromic prime (111 is not a prime)
output = []
while count < 100:
if is_palindrome(i) and is_prime(i):
output.append(i)
count += 1
i += 2 # Skip even numbers because all even numbers except 2 are not primes
# Display the results with 10 per line separated by spaces
for index, num in enumerate(output, start=1):
print(f'{num}', end=' ' if index % 10 != 9 else '\n')
阅读全文