题解 | #质数的计数#
质数的计数
https://www.nowcoder.com/practice/190167d1990442da9adb133980259a27
# 埃式筛
# 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
#
#
# @param n int整型
# @return int整型
#
class Solution:
def primesCount(self , n: int) -> int:
# write code here
if n<2:
return 0
res=[1]*n
res[0]=res[1]=0
for i in range(2,int(n**0.5)+1):
if res[i]==1:
res[2*i:n:i]=[0]*int(((n-i-1)/i))
return res.count(1)
查看9道真题和解析