枫雪天,天生恶相,巫术探索
统计所有小于非负整数 n 的质数的数量。
示例:
输入: 10 输出: 4 解释: 小于 10 的质数一共有 4 个, 它们是 2, 3, 5, 7 。
class solution:
def countprimes(self, n):
"""
:type n: int
:rtype: int
"""
isprime = [1] * max(2, n)
isprime[0],isprime[1]=false,false
x = 2
while x * x < n:
if isprime[x]:
p = x * x
while p < n:
isprime[p] = 0
p += x
x +=1
return (sum(isprime))
参考: https://en.wikipedia.org/wiki/sieve_of_eratosthenes
如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复
Python 实现将numpy中的nan和inf,nan替换成对应的均值
python爬虫把url链接编码成gbk2312格式过程解析
网友评论