Skip to content

Commit 170898a

Browse files
author
oushihao
committed
feat(count-primes): 枚举 | 埃氏筛
1 parent 5e4aa13 commit 170898a

File tree

7 files changed

+177
-0
lines changed

7 files changed

+177
-0
lines changed

assets/data/category.json

+12
Original file line numberDiff line numberDiff line change
@@ -603,6 +603,12 @@
603603
"path": "../../problemset/factorial-trailing-zeroes/README.md",
604604
"difficulty": "中等"
605605
},
606+
{
607+
"id": 204,
608+
"title": "计数质数",
609+
"path": "../../problemset/count-primes/README.md",
610+
"difficulty": "中等"
611+
},
606612
{
607613
"id": 258,
608614
"title": "各位相加",
@@ -1982,6 +1988,12 @@
19821988
{
19831989
"label": "枚举",
19841990
"problems": [
1991+
{
1992+
"id": 204,
1993+
"title": "计数质数",
1994+
"path": "../../problemset/count-primes/README.md",
1995+
"difficulty": "中等"
1996+
},
19851997
{
19861998
"id": 1601,
19871999
"title": "最多可达成的换楼请求数目",

assets/data/problems.json

+10
Original file line numberDiff line numberDiff line change
@@ -1789,6 +1789,16 @@
17891789
"url": "https://leetcode-cn.com/problems/remove-linked-list-elements/",
17901790
"path": "../../problemset/remove-linked-list-elements/README.md"
17911791
},
1792+
{
1793+
"id": 204,
1794+
"title": {
1795+
"cn": "计数质数",
1796+
"en": "count-primes"
1797+
},
1798+
"difficulty": "中等",
1799+
"url": "https://leetcode-cn.com/problems/count-primes/",
1800+
"path": "../../problemset/count-primes/README.md"
1801+
},
17921802
{
17931803
"id": 258,
17941804
"title": {

assets/docs/CATEGORY.md

+2
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,7 @@
125125
| 169. [多数元素](../../problemset/majority-element/README.md) | 简单 |
126126
| 171. [Excel 表列序号](../../problemset/excel-sheet-column-number/README.md) | 简单 |
127127
| 172. [阶乘后的零](../../problemset/factorial-trailing-zeroes/README.md) | 中等 |
128+
| 204. [计数质数](../../problemset/count-primes/README.md) | 中等 |
128129
| 258. [各位相加](../../problemset/add-digits/README.md) | 简单 |
129130
| 504. [七进制数](../../problemset/base-7/README.md) | 简单 |
130131
| 553. [最优除法](../../problemset/optimal-division/README.md) | 中等 |
@@ -409,6 +410,7 @@
409410

410411
| 题目 | 难度 |
411412
| ---- | ---- |
413+
| 204. [计数质数](../../problemset/count-primes/README.md) | 中等 |
412414
| 1601. [最多可达成的换楼请求数目](../../problemset/maximum-number-of-achievable-transfer-requests/README.md) | 困难 |
413415

414416
## 位运算

assets/docs/PROBLEMS.md

+2
Original file line numberDiff line numberDiff line change
@@ -358,6 +358,8 @@
358358

359359
[203. 移除链表元素](../../problemset/remove-linked-list-elements/README.md)
360360

361+
[204. 计数质数](../../problemset/count-primes/README.md)
362+
361363
[258. 各位相加](../../problemset/add-digits/README.md)
362364

363365
[300. 最长递增子序列](../../problemset/longest-increasing-subsequence/README.md)

problemset/count-primes/README.md

+82
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
# 计数质数
2+
3+
> 难度:中等
4+
>
5+
> https://leetcode-cn.com/problems/count-primes/
6+
7+
## 题目
8+
9+
给定整数 `n`**返回** 所有小于非负整数  `n`  的质数的数量 。
10+
11+
### 示例
12+
13+
#### 示例 1:
14+
15+
```
16+
输入:n = 10
17+
输出:4
18+
解释:小于 10 的质数一共有 4 个, 它们是 2, 3, 5, 7 。
19+
```
20+
21+
#### 示例 2:
22+
23+
```
24+
输入:n = 0
25+
输出:0
26+
```
27+
28+
#### 示例 3:
29+
30+
```
31+
输入:n = 1
32+
输出:0
33+
```
34+
35+
## 解题
36+
37+
### 枚举
38+
39+
```typescript
40+
/**
41+
* 枚举
42+
* @desc 时间复杂度 O(N√N) 空间复杂度 O(1)
43+
* @param n
44+
*/
45+
export function countPrimes(n: number): number {
46+
let ans = 0;
47+
for (let i = 2; i < n; i++) isPrimes(i) && ans++;
48+
return ans;
49+
50+
function isPrimes(x: number): boolean {
51+
for (let i = 2; i * i <= x; i++) {
52+
if (x % i === 0) return false;
53+
}
54+
return true;
55+
}
56+
}
57+
```
58+
59+
### 埃氏筛
60+
61+
```typescript
62+
/**
63+
* 埃氏筛
64+
* @desc 时间复杂度 O(Nlog(logN)) 空间复杂度 O(N)
65+
* @param n
66+
*/
67+
export function countPrimes2(n: number): number {
68+
const isPrime = new Array(n).fill(true);
69+
70+
let ans = 0;
71+
for (let i = 2; i < n; i++) {
72+
if (isPrime[i]) {
73+
ans += 1;
74+
for (let j = i * i; j < n; j += i) {
75+
isPrime[j] = false;
76+
}
77+
}
78+
}
79+
80+
return ans;
81+
}
82+
```

problemset/count-primes/index.spec.ts

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import { countPrimes, countPrimes2 } from '.';
2+
3+
describe('计数质数', () => {
4+
describe('枚举', () => {
5+
testCase(countPrimes);
6+
});
7+
8+
describe('埃氏筛', () => {
9+
testCase(countPrimes2);
10+
});
11+
});
12+
13+
function testCase(fn: (n: number) => number) {
14+
it('示例一', () => {
15+
const n = 10;
16+
const expected = 4;
17+
expect(fn(n)).toBe(expected);
18+
});
19+
20+
it('示例二', () => {
21+
const n = 0;
22+
const expected = 0;
23+
expect(fn(n)).toBe(expected);
24+
});
25+
26+
it('示例三', () => {
27+
const n = 1;
28+
const expected = 0;
29+
expect(fn(n)).toBe(expected);
30+
});
31+
}

problemset/count-primes/index.ts

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/**
2+
* 枚举
3+
* @desc 时间复杂度 O(N√N) 空间复杂度 O(1)
4+
* @param n
5+
*/
6+
export function countPrimes(n: number): number {
7+
let ans = 0;
8+
for (let i = 2; i < n; i++) isPrimes(i) && ans++;
9+
return ans;
10+
11+
function isPrimes(x: number): boolean {
12+
for (let i = 2; i * i <= x; i++) {
13+
if (x % i === 0) return false;
14+
}
15+
return true;
16+
}
17+
}
18+
19+
/**
20+
* 埃氏筛
21+
* @desc 时间复杂度 O(Nlog(logN)) 空间复杂度 O(N)
22+
* @param n
23+
*/
24+
export function countPrimes2(n: number): number {
25+
const isPrime = new Array(n).fill(true);
26+
27+
let ans = 0;
28+
for (let i = 2; i < n; i++) {
29+
if (isPrime[i]) {
30+
ans += 1;
31+
for (let j = i * i; j < n; j += i) {
32+
isPrime[j] = false;
33+
}
34+
}
35+
}
36+
37+
return ans;
38+
}

0 commit comments

Comments
 (0)