多组输入,每行输入一个正整数(不大于100)。
针对每行输入的整数n,输出两行,第一行,输出n之内(包括n)的素数,用空格分隔,
第二行,输出数组中2之后被清0 的个数。每行输出后换行。
20
2 3 5 7 11 13 17 19 11
n=int(input()) if n>=7: c=[2,3,5,7] count=-4 elif 5<=n<7: c=[2,3,5] count=-3 elif 3<=n<5: c=[2,3] count=-2 elif 2<=n<3: c=[2] count=-1 else : c=[] count=0 for i in range(2,n+1): if i%2==0&nbs***bsp;i%3==0&nbs***bsp;i%4==0&nbs***bsp;i%5==0&nbs***bsp;i%6==0&nbs***bsp;i%7==0&nbs***bsp;i%8==0&nbs***bsp;i%9==0: count+=1 else: c.append(i) print(*c) print(count)傻瓜解法
def prime(n): for i in range(2,n):# 优化写法 n ==> int(n**0.5)+1 if n%i ==0 :#只要有任何一次整除了,就不是素数,返回False return False #如果都没有跳出,就返回正确 return True if __name__ == '__main__': n = eval(input()) list_n = [i for i in range(2,n+1)] list_new = [] for i in list_n: res = prime(i) if res is True: list_new.append(i) print(*list_new) print(len(list_n)-len(list_new)) #爱图图
while True: try: listx = [] list1 = [] list3 = [] b= 2 sum = 0 a = int(input()) for i in range(2,a+1): listx.append(i) while b<a+1 : for i in range(2,a+1): if i > b and i % b == 0: list1.append(i) b += 1 list3 = list(set(listx) - set(list1)) #list3.sort() for i in list3: print(i,end=" ") print("") print("%d" %(a-len(list3)-1)) except: break 这个37为啥在第四个出现