Skip to content

Commit 20f2676

Browse files
nartherionVolodymyr Sachko
authored andcommitted
Bug in range sieve when left boundry is 0
In case when `L = 0` and `R > 0`, algorithm will say that 0 and 1 are primes. Index initialization improvement
1 parent 0da3b79 commit 20f2676

File tree

1 file changed

+13
-8
lines changed

1 file changed

+13
-8
lines changed

src/algebra/sieve-of-eratosthenes.md

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,7 @@ To solve such a problem, we can use the idea of the Segmented sieve.
207207
We pre-generate all prime numbers up to $\sqrt R$, and use those primes to mark all composite numbers in the segment $[L, R]$.
208208
209209
```cpp
210-
vector<char> segmentedSieve(long long L, long long R) {
210+
vector<char> segmented_sieve(long long L, long long R) {
211211
// generate all primes up to sqrt(R)
212212
long long lim = sqrt(R);
213213
vector<char> mark(lim + 1, false);
@@ -222,10 +222,12 @@ vector<char> segmentedSieve(long long L, long long R) {
222222
223223
vector<char> isPrime(R - L + 1, true);
224224
for (long long i : primes)
225-
for (long long j = max(i * i, (L + i - 1) / i * i); j <= R; j += i)
225+
for (long long j = max(i, (L + i - 1) / i) * i; j <= R; j += i)
226226
isPrime[j - L] = false;
227-
if (L == 1)
228-
isPrime[0] = false;
227+
if (L == 0)
228+
isPrime[L] = false;
229+
if (L <= 1)
230+
isPrime[min(1 - L, R - L)] = false;
229231
return isPrime;
230232
}
231233
```
@@ -234,14 +236,16 @@ Time complexity of this approach is $O((R - L + 1) \log \log (R) + \sqrt R \log
234236
It's also possible that we don't pre-generate all prime numbers:
235237

236238
```cpp
237-
vector<char> segmentedSieveNoPreGen(long long L, long long R) {
239+
vector<char> segmented_sieve_no_pre_gen(long long L, long long R) {
238240
vector<char> isPrime(R - L + 1, true);
239241
long long lim = sqrt(R);
240242
for (long long i = 2; i <= lim; ++i)
241-
for (long long j = max(i * i, (L + i - 1) / i * i); j <= R; j += i)
243+
for (long long j = max(i, (L + i - 1) / i) * i; j <= R; j += i)
242244
isPrime[j - L] = false;
243-
if (L == 1)
244-
isPrime[0] = false;
245+
if (L == 0)
246+
isPrime[L] = false;
247+
if (L <= 1)
248+
isPrime[min(1 - L, R - L)] = false;
245249
return isPrime;
246250
}
247251
```
@@ -258,6 +262,7 @@ However, this algorithm also has its own weaknesses.
258262
259263
* [Leetcode - Four Divisors](https://leetcode.com/problems/four-divisors/)
260264
* [Leetcode - Count Primes](https://leetcode.com/problems/count-primes/)
265+
* [Leetcode - Closest Prime Numbers in Range](https://leetcode.com/problems/closest-prime-numbers-in-range/)
261266
* [SPOJ - Printing Some Primes](http://www.spoj.com/problems/TDPRIMES/)
262267
* [SPOJ - A Conjecture of Paul Erdos](http://www.spoj.com/problems/HS08PAUL/)
263268
* [SPOJ - Primal Fear](http://www.spoj.com/problems/VECTAR8/)

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy