求一百以内质数怎么编程
时间: 2023-05-21 16:02:34 浏览: 140
求100以内的质数程序
可以使用筛法求解一百以内的质数。以下是示例代码:
```python
def sieve_of_eratosthenes(n):
primes = [True] * (n+1)
primes[0] = primes[1] = False
for i in range(2, int(n**0.5)+1):
if primes[i]:
for j in range(i*i, n+1, i):
primes[j] = False
return [i for i in range(n+1) if primes[i]]
primes = sieve_of_eratosthenes(100)
print(primes)
```
输出结果为:
```
[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]
```
阅读全文