如何用Python找出前50个素数,并输出每行输出六个
时间: 2024-05-14 21:14:12 浏览: 34
素数是指只能被1和自身整除的整数,因此可以用一个循环来判断一个数是否为素数。以下是用Python实现找出前50个素数的代码,并每行输出六个素数:
```python
def is_prime(num):
if num <= 1:
return False
for i in range(2, int(num**0.5)+1):
if num % i == 0:
return False
return True
count = 0
num = 2
while count < 50:
if is_prime(num):
print(num, end=' ')
count += 1
if count % 6 == 0:
print()
num += 1
```
输出结果为:
```
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
```
相关问题
python编程 找出前50个素数并输出,每行输出六个
以下是 Python 代码,可以找出前50个素数并每行输出六个:
```python
def is_prime(n):
if n < 2:
return False
for i in range(2, int(n ** 0.5) + 1):
if n % i == 0:
return False
return True
count = 0
num = 2
while count < 50:
if is_prime(num):
print(num, end=' ')
count += 1
if count % 6 == 0:
print()
num += 1
```
输出结果如下:
```
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
```
用python找出前五十个素数并输出,每行输出六个
以下是用 Python 找出前五十个素数并输出的代码:
```python
def is_prime(n):
if n <= 1:
return False
for i in range(2, int(n**0.5)+1):
if n % i == 0:
return False
return True
count = 0
num = 2
while count < 50:
if is_prime(num):
print(num, end=' ')
count += 1
if count % 6 == 0:
print()
num += 1
```
输出结果如下:
```
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
```
希望能够帮到你!