1 min read

how to judge a number is a prime

栾佳硕的作业:

解密题目2:

编写一个程序,让它输出1-200之间的所有质数。(解密点50)

解密点(20/100)

#定义一个判断数字是否为质数的函数
def is_prime(number):
    if number == 1 :
        return False
    if number == 2 :
        return True
    if number >= 3 :
        a = True
        for i in range(2,number):
            if number % i == 0 :
                a = False
                break
        return a

            
list1 = []
number =200
for i in range(1,number+1) :
    if is_prime(i) == True :
        list1.append(i)
#质数为:
print(list1)
## [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]