# python（2）输出100以内的素数

#100以内素数

import math    #如同c++中的#include<math.h>
def prime(n):  #定义带一个参数n的函数
number=[]  #定义空数组用来记录位置上的数是素数True，不是False
prime_number=[]  #定义空数组用来储存素数
for i in range(0,n,1):   #遍历0到n，数间隔为1
number.append(True) #先将number[0]到number[n]全部赋值为True
number[0]=False
number[1]=False
for i in range(2,int(math.sqrt(n))):
for j in range(i*i,n,i):          #这种算法将2,3,4.....sqrt(n)的倍数
#的数全部取出来
print(j)    #理解程序用
number[j]=False
for i in range(0,n,1):
if number[i]:                   #如果numbe[i]=True
prime_number.append(i)   #则将i添加到prime_number数组中
return(prime_number)                    #返回prime_number

ans=prime(100)
print(len(ans))  #记录素数的个数
print(ans)

>>>
[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 i in range(2,int(math.sqrt(n))):
for j in range(i*i,n,i): 

>>>
RESTART: C:\Users\xiaohengpeng\Desktop\pythontest\HomeWork\2-prinme in 1000sushu.py
4
6
8
10
12
14
16
18
20
22
24
26
28
30
32
34
36
38
40
42
44
46
48
9
12
15
18
21
24
27
30
33
36
39
42
45
48
16
20
24
28
32
36
40
44
48
25
30
35
40
45
36
42
48
16
[2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 49]
>>> 

#1000以内素数

import math
def is_prime(n):

if n==1:
return False
for i in range(2,int(math.sqrt(n))+1):
if n % i == 0:
return False
else:
return True

count=0
number=[]
for i in range(0,1000,1):
if is_prime(i):
number.append(i)
count += 1
print('{}:{}'.format(count,i))  #碰到的新语句，可删除

print(number)


>
[0, 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, 307, 311, 313, 317, 331, 337, 347, 349, 353, 359, 367, 373, 379, 383, 389, 397, 401, 409, 419, 421, 431, 433, 439, 443, 449, 457, 461, 463, 467, 479, 487, 491, 499, 503, 509, 521, 523, 541, 547, 557, 563, 569, 571, 577, 587, 593, 599, 601, 607, 613, 617, 619, 631, 641, 643, 647, 653, 659, 661, 673, 677, 683, 691, 701, 709, 719, 727, 733, 739, 743, 751, 757, 761, 769, 773, 787, 797, 809, 811, 821, 823, 827, 829, 839, 853, 857, 859, 863, 877, 881, 883, 887, 907, 911, 919, 929, 937, 941, 947, 953, 967, 971, 977, 983, 991, 997]