Python怎么打印列表中的素数并输出素数的个数?

如题所述

1、实例源码
def showSu():
a = [2, 4, 5, 6, 7, 8, 3, 11, 13]
b = 0
for i in range(0, len(a)):
if a[i] > 1:
for j in range(2, a[i]):
if a[i] % j == 0:
break
else:
b += 1
print(a[i])
print("素数个数:%d"%b)

showSu()

2、实例结果
2
5
7
3
11
13
素数个数:6
温馨提示:答案为网友推荐,仅供参考
第1个回答  2020-11-04
# -*- coding:utf-8 -*-
s = 0
for x in 列表:
x = int(x)
temp = 0
for x1 in range(1, x):
if x % x1 == 0:
temp += 1
if temp <= 1:
print("素数:" + str(x))
s += 1
print("素数个数:" + str(x)
相似回答