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
Copy file name to clipboardexpand all lines: README.md
+1
Original file line number
Diff line number
Diff line change
@@ -49,6 +49,7 @@ a set of rules that precisely define a sequence of operations.
49
49
*[Euclidean Algorithm](https://github.com/trekhleb/javascript-algorithms/tree/master/src/algorithms/math/euclidean-algorithm) - calculate the Greatest Common Divisor (GCD)
50
50
*[Least Common Multiple](https://github.com/trekhleb/javascript-algorithms/tree/master/src/algorithms/math/least-common-multiple) (LCM)
*[Sieve of Eratosthenes](https://github.com/trekhleb/javascript-algorithms/tree/master/src/algorithms/math/sieve-of-eratosthenes) - finding all prime numbers up to any given limit
52
53
***Sets**
53
54
*[Cartesian Product](https://github.com/trekhleb/javascript-algorithms/tree/master/src/algorithms/sets/cartesian-product) - product of multiple sets
54
55
*[Power Set](https://github.com/trekhleb/javascript-algorithms/tree/master/src/algorithms/sets/power-set) - all subsets of a set
Copy file name to clipboardexpand all lines: src/algorithms/math/sieve-of-eratosthenes/README.md
+8-5
Original file line number
Diff line number
Diff line change
@@ -6,15 +6,18 @@ It is attributed to Eratosthenes of Cyrene, an ancient Greek mathematician.
6
6
7
7
## How it works
8
8
9
-
1. Create a boolean array of `n+1` positions (to represent the numbers `0` through `n`)
9
+
1. Create a boolean array of `n + 1` positions (to represent the numbers `0` through `n`)
10
10
2. Set positions `0` and `1` to `false`, and the rest to `true`
11
11
3. Start at position `p = 2` (the first prime number)
12
-
4. Mark as `false` all the multiples of `p` (that is, positions `2*p`, `3*p`, `4*p`... until you reach the end of the array)
12
+
4. Mark as `false` all the multiples of `p` (that is, positions `2 * p`, `3 * p`, `4 * p`... until you reach the end of the array)
13
13
5. Find the first position greater than `p` that is `true` in the array. If there is no such position, stop. Otherwise, let `p` equal this new number (which is the next prime), and repeat from step 4
14
14
15
-
When the algorithm terminates, the numbers remaining `true` in the array are all the primes below `n`.
15
+
When the algorithm terminates, the numbers remaining `true` in the array are all
16
+
the primes below `n`.
16
17
17
-
An improvement of this algorithm is, in step 4, start marking multiples of `p` from `p*p`, and not from `2*p`. The reason why this works is because, at that point, smaller multiples of `p` will have already been marked `false`.
18
+
An improvement of this algorithm is, in step 4, start marking multiples
19
+
of `p` from `p * p`, and not from `2 * p`. The reason why this works is because,
20
+
at that point, smaller multiples of `p` will have already been marked `false`.
18
21
19
22
## Example
20
23
@@ -26,4 +29,4 @@ The algorithm has a complexity of `O(n log(log n))`.
0 commit comments