判断101-200之间有多少个素数,并输出所有素数。
时间: 2023-12-17 20:52:17 浏览: 71
这个问题可以使用筛法求解素数,这里给出一个 Python 代码实现:
```python
def count_primes(start, end):
primes = []
is_prime = [True] * (end+1)
p = 2
while p * p <= end:
if is_prime[p]:
for i in range(p*2, end+1, p):
is_prime[i] = False
p += 1
for i in range(max(start, 2), end+1):
if is_prime[i]:
primes.append(str(i))
return len(primes), ', '.join(primes)
count, primes = count_primes(101, 200)
print(f"There are {count} prime numbers between 101 and 200:\n{primes}")
```
运行结果:
```
There are 21 prime numbers between 101 and 200:
101, 103, 107, 109, 113, 127, 131, 137, 139, 149, 151, 157, 163, 167, 173, 179, 181, 191, 193, 197, 199
```
阅读全文