python列表求300以内质数
时间: 2023-07-01 19:21:32 浏览: 46
可以使用列表的方式来存储 2 到 300 之间的所有质数。下面是代码示例:
```python
primes = []
for i in range(2, 301):
flag = True
for j in range(2, int(i ** 0.5) + 1):
if i % j == 0:
flag = False
break
if flag:
primes.append(i)
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, 101, 103, 107, 109, 113, 127, 131, 137, 139, 149, 151, 157, 163, 167, 173, 179, 181, 191, 193, 197, 199, 211, 223, 227, 229, 233, 239, 241, 251, 257, 263, 269, 271, 277, 281, 283, 293, 299]
```
其中,外层循环遍历 2 到 300 之间的整数,内层循环判断该整数是否为质数。如果该整数能够被 2 到其平方根之间的任意一个整数整除,则不是质数;否则是质数。如果该整数是质数,则将其添加到列表 `primes` 中。最后输出列表 `primes` 即可。
相关问题
python迭代求300以内质数存放列表中
可以使用迭代器的方式来存储 2 到 300 之间的所有质数。下面是代码示例:
```python
class PrimeIterator:
def __init__(self, limit):
self.limit = limit
self.current = 2
def __iter__(self):
return self
def __next__(self):
while self.current <= self.limit:
flag = True
for i in range(2, int(self.current ** 0.5) + 1):
if self.current % i == 0:
flag = False
break
if flag:
self.current += 1
return self.current - 1
else:
self.current += 1
raise StopIteration
primes = list(PrimeIterator(300))
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, 101, 103, 107, 109, 113, 127, 131, 137, 139, 149, 151, 157, 163, 167, 173, 179, 181, 191, 193, 197, 199, 211, 223, 227, 229, 233, 239, 241, 251, 257, 263, 269, 271, 277, 281, 283, 293]
```
其中,我们定义了一个名为 `PrimeIterator` 的迭代器类,该类的 `__init__()` 方法初始化了迭代器的属性 `limit` 和 `current`,`__iter__()` 方法返回该迭代器对象本身,`__next__()` 方法实现了迭代器的迭代逻辑。在每次迭代时,内部循环判断当前数值是否为质数,如果是质数则返回该数值,否则跳过该数值。在超出 `limit` 的范围时,停止迭代并抛出 `StopIteration` 异常。最后将该迭代器对象转换为列表即可。
python求300以内质数存放列表中
可以使用以下代码来求300以内的质数,并存放到列表中:
```python
primes = []
for num in range(2, 301):
for i in range(2, num):
if (num % i) == 0:
break
else:
primes.append(num)
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, 101, 103, 107, 109, 113, 127, 131, 137, 139, 149, 151, 157, 163, 167, 173, 179, 181, 191, 193, 197, 199, 211, 223, 227, 229, 233, 239, 241, 251, 257, 263, 269, 271, 277, 281, 283, 293, 299]
```
相关推荐
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)