Skip to content
Navigation Menu
Sign in
Products
Platform
GitHub
Repository hosting, code review, and collaboration
Copilot
AI coding agent for tasks, projects and automated work
Build & Ship
Actions
CI/CD and automation
Code Quality
Quality analysis, fixes, and enforcement
Codespaces
Cloud development environments
Secure
Code Security
Vulnerability detection and remediation
Secret Protection
Secret scanning and leak prevention
All Features
Developers
Learn
Documentation
Training
Certifications
Build
Build with Copilot
Customize Copilot
Integrations
Explore
Trending repos
GitHub Sponsors
Developer articles
Changelog
Changelog
Your source for the latest features, improvements, and fixes across GitHub products, all in one place.
Businesses
By Stage
Enterprises
Team
Startups
Nonprofits
By Industry
Healthcare
Financial services
Manufacturing
Government
See all industries
Evaluate GitHub
Why GitHub
Customer Stories
Business insights
Trust Center
Add-ons
GitHub Advanced Security
Enterprise-grade code security
Copilot for business
Enterprise-grade AI features
Premium Support
Enterprise-grade 24/7 support
Resources
Learn & Connect
Documentation
Resource center
Blog
Events & webinars
Community Forum
Customer Stories
Integrations
Support
Customer support
Training
GitHub Status
Company
About
Partners
Changelog
GitHub Universe
GitHub Universe
The event for developers, by developers — talks, workshops, and what’s next for GitHub.
Pricing
Search
/
Sign in
Sign up
Appearance settings
You signed in with another tab or window.
Reload
to refresh your session.
You signed out in another tab or window.
Reload
to refresh your session.
You switched accounts on another tab or window.
Reload
to refresh your session.
Dismiss alert
{{ message }}
OnlyChristmas
leetcode
Repository navigation
Code
Issues
Pull requests
Actions
Projects
Security and quality
Insights
More
items
Files
Expand file tree
master
Breadcrumbs
leetcode
/
Python
/
count-primes.py
Copy path
Blame
More file actions
Blame
More file actions
Latest commit
History
History
History
38 lines (32 loc) · 1.56 KB
master
Breadcrumbs
leetcode
/
Python
/
count-primes.py
Copy path
Top
File metadata and controls
Code
Blame
38 lines (32 loc) · 1.56 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
'''
Count the number of prime numbers less than a non-negative number, n.
Example:
Input: 10
Output: 4
Explanation: There are 4 prime numbers less than 10, they are 2, 3, 5, 7.
'''
class Solution:
def countPrimes(self, n: int) -> int:
# 思路一: 简单的遍历除以小于他的数,效率太低了!
# 思路二: 遍历除以小于他一半的数,有率提升一倍
# 思路三: 动态建立素数表,这样居然还不行?!效率还是低
# num = 0
# result = [2 ]
# for i in range(2, n):
# for j in range(len(result)):
# if i % result[j] == 0 and i!=result[j]:
# break
# if j==len(result)-1:
# result.append(i)
# num += 1
# return num
# 思路四: Sieve of Eratosthenes(埃拉托色尼筛法,简称埃氏筛法),参考维基百科:https://en.wikipedia.org/wiki/Sieve_of_Eratosthenes
# 0 表示不是素数, 1 代表素数. 所以总和是多少,最后就有多少个
if n < 3: # 注意审题边界条件,是小于n,不包括n
return 0 # 注意 数组越界的情况
primes = [1] * n
primes[0] = primes[1] = 0
for i in range(2, int(n**0.5)+1): # 在选择除数时候的一个小技巧.大于一半的数是不可能做除数的
if primes[i]:
primes[i * i: n: i] = [0] * ((n - 1) // i - i + 1) # 用埃氏筛法, 将每一个不是素数的数筛选掉.大大减少除法的数量.
return sum(primes)
You can’t perform that action at this time.