质数(prime number)又称素数,有无限个。质数定义为在大于1的自然数中,除了1和它本身以外不再有其他因数,这样的数称为质数
1 list1=[] 2 num=0 3 4 for i in range(2,100): 5 for j in range(2,int(i**0.5)+1): 6 print(int(i**0.5)+1) 7 if i%j==0: 8 break 9 else: 10 list1.append(i) 11 num+=1 12 print(list1) 13 print(num)
优化
list1=[2] num=0 for i in range(2,100): for j in list1: if i%j==0: break else: list1.append(i) num+=1 print(list1) print(num)