python 输出100以内的素数,以 “ 是素数”形式输出
时间: 2023-09-02 20:07:44 浏览: 38
可以使用以下代码来输出100以内的素数,以 “ 是素数”形式输出:
```python
for num in range(2, 101):
for i in range(2, num):
if num % i == 0:
break
else:
print(num, "是素数")
```
输出结果为:
```
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 是素数
```
相关问题
python输出100以内素数
以下是Python输出100以内素数的方法:
```python
primes = []
for num in range(2, 101):
for i in range(2, num):
if (num % i) == 0:
break
else:
primes.append(num)
print("100以内的素数为:", primes)
```
输出结果为:
```
100以内的素数为: [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]
```
解释:这段代码使用了两个for循环,第一个for循环遍历2到100之间的所有数字,第二个for循环遍历2到当前数字之间的所有数字,如果当前数字能够被其中任意一个数字整除,则说明不是素数,跳出循环;否则将其加入素数列表中。
用python输出100以内素数
以下是用 Python 输出100以内的素数的代码:
```python
# 判断数字是否为素数函数
def is_prime(num):
if num < 2:
return False
for i in range(2, int(num**0.5)+1):
if num % i == 0:
return False
return True
# 输出100以内的素数
for i in range(2, 100):
if is_prime(i):
print(i, end=' ')
```
输出结果为:
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
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.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)