输出m到n之间的所有素数,每行5个,并求和
时间: 2023-08-31 13:10:10 浏览: 129
PTA实验LB05-LB08.pdf
好的,让我来帮您实现。以下是代码:
```python
import math
def is_prime(num):
if num < 2:
return False
for factor in range(2, int(math.sqrt(num))+1):
if num % factor == 0:
return False
return True
m, n = 1, 100 # 这里可根据需求修改m和n的值
count = 0
sums = 0
for num in range(m, n+1):
if is_prime(num):
if count == 5:
print()
count = 0
print(num, end="\t")
count += 1
sums += num
print("\nsums:", sums)
```
输出结果为:
```
2 3 5 7 11
13 17 19 23 29
31 37 41 43 47
53 59 61 67 71
73 79 83 89 97
sums: 1060
```
请问是否满意答案?
阅读全文