Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit4645c92

Browse files
committed
rename the linear sieve and update section about speed
closes#756
1 parent2313f8d commit4645c92

File tree

3 files changed

+15
-12
lines changed

3 files changed

+15
-12
lines changed

‎src/algebra/prime-sieve-linear.md

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
<!--?titleSieve of Eratosthenes WithLinearTime Complexity-->
1+
<!--?title LinearSieve-->
22

3-
#Sieve of Eratosthenes HavingLinearTime Complexity
3+
#LinearSieve
44

55
Given a number $n$, find all prime numbers in a segment $[2;n]$.
66

@@ -14,7 +14,8 @@ The weakness of the given algorithm is in using more memory than the classic sie
1414

1515
Thus, it makes sense to use the described algorithm only until for numbers of order $10^7$ and not greater.
1616

17-
The algorithm's authorship appears to belong to Gries & Misra (Gries, Misra, 1978: see references in the end of the article). And, strictly speaking, this algorithm shouldn't be called "sieve of Eratosthenes" since it's too different from the classic one.
17+
The algorithm's authorship appears to belong to Gries & Misra (Gries, Misra, 1978: see references in the end of the article).
18+
However it can also be attributed to Euler, and is also known as Euler's Sieve, who already used a similar version of it during his work.
1819

1920
##Algorithm
2021

@@ -43,21 +44,20 @@ The proof of correctness of this algorithm and its runtime can be found after th
4344

4445
```cpp
4546
constint N =10000000;
46-
int lp[N+1];
47+
vector<int>lp(N+1);
4748
vector<int> pr;
4849

49-
for (int i=2; i<=N; ++i) {
50+
for (int i=2; i <=N; ++i) {
5051
if (lp[i] == 0) {
5152
lp[i] = i;
52-
pr.push_back(i);
53+
pr.push_back(i);
5354
}
54-
for (int j=0; j<(int)pr.size() && pr[j]<=lp[i] && i*pr[j]<=N; ++j)
55+
for (int j=0; j <(int)pr.size() && pr[j] <=lp[i] && i*pr[j] <=N; ++j) {
5556
lp[i * pr[j]] = pr[j];
57+
}
5658
}
5759
```
5860
59-
We can speed it up a bit by replacing vector $pr$ with a simple array and a counter, and by getting rid of the second multiplication in the nested`for` loop (for that we just need to remember the product in a variable).
60-
6161
## Correctness Proof
6262
6363
We need to prove that the algorithm sets all values $lp []$ correctly, and that every value will be set exactly once. Hence, the algorithm will have linear runtime, since all the remaining actions of the algorithm, obviously, work for $O (n)$.
@@ -76,7 +76,10 @@ Hence, the algorithm will go through every composite number exactly once, settin
7676
7777
## Runtime and Memory
7878
79-
Although the running time of $O(n)$ is better than $O(n \log \log n)$ of the classic sieve of Eratosthenes, the difference between them is not so big. In practice that means just double difference in speed, and the optimized versions of the sieve run as fast as the algorithm given here.
79+
Although the running time of $O(n)$ is better than $O(n \log \log n)$ of the classic sieve of Eratosthenes, the difference between them is not so big.
80+
In practice the linear sieve runs about as fast as a typical implementation of the sieve of Eratosthenes.
81+
82+
In comparison to optimized versions of the sieve of Erathosthenes, e.g. the segmented sieve, it is much slower.
8083
8184
Considering the memory requirements of this algorithm - an array $lp []$ of length $n$, and an array of $pr []$ of length $\frac n {\ln n}$, this algorithm seems to worse than the classic sieve in every way.
8285

‎src/algebra/sieve-of-eratosthenes.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -242,7 +242,7 @@ Obviously, the complexity is worse, which is $O((R - L + 1) \log (R) + \sqrt R)$
242242
## Linear time modification
243243
244244
We can modify the algorithm in a such a way, that it only has linear time complexity.
245-
This approach is described in the article [Sieve of Eratosthenes HavingLinearTime Complexity](./algebra/prime-sieve-linear.html).
245+
This approach is described in the article [LinearSieve](./algebra/prime-sieve-linear.html).
246246
However, this algorithm also has its own weaknesses.
247247
248248
## Practice Problems

‎src/index.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ and adding new articles to the collection.*
2121
-[Fibonacci Numbers](./algebra/fibonacci-numbers.html)
2222
-**Prime numbers**
2323
-[Sieve of Eratosthenes](./algebra/sieve-of-eratosthenes.html)
24-
-[Sieve of Eratosthenes WithLinearTime Complexity](./algebra/prime-sieve-linear.html)
24+
-[LinearSieve](./algebra/prime-sieve-linear.html)
2525
-[Primality tests](./algebra/primality_tests.html)
2626
-[Integer factorization](./algebra/factorization.html)
2727
-**Number-theoretic functions**

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp