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
+8-9Lines changed: 8 additions & 9 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -117,7 +117,7 @@ After that we just iterate over B and get the highest value. Even further, we do
117
117
118
118
## 9. Counting factors
119
119
120
-
Given N, we know that if a N is divisible by a number X, then N / X also is a divisor of N.
120
+
Given N, we know that if a N is divisible by a number X, then N / X also is a divisor of N.<br/>
121
121
_I.e.:_`N % X = 0` → `N = X * Y` → `Y = N / X` → `N % Y = 0`
122
122
123
123
We also know at least X or Y is lesser or equal to sqrt(N) (otherwise X \* Y > N). Thus we can improve a counter algorithm from being **O(N)** to **O(sqrt(N))**, and incrementing the counter by 2 each time we find a match (because we are adding both X and Y) or 1 if X == sqrt(N) (otherwise we would add X twice to the counter).
@@ -156,15 +156,14 @@ An algorithm that returns a boolean array P of length n, where if i is a prime n
156
156
- First, we create P and fill it with the value true (i.e., we assume all numbers are prime)
157
157
- Initially, from an outer loop, we iterate each number i in range [ 2, 3, 4, …, N ], marking every multiple of i as `false` (this will have an O(Nˆ2) cost).
158
158
159
-

- To improve this algorithm, we only need to iterate over numbers i where `P[i] === true` (i.e., numbers that still haven't been marked non-prime by previous steps)
162
-
- As we've seen in _Counting factors_, we only need to iterate i over [ 2, …, sqrt(N) ]
163
-
- To further improve this, notice how for a number i, we don't need to verify multiples smaller than i^2, because these numbers already been squashed in previous steps (by the other factor of said numbers)
161
+
- To improve this algorithm, we only need to iterate over numbers i where `P[i] === true` (i.e., numbers that still haven't been marked non-prime by previous steps)
162
+
- As we've seen in _Counting factors_, we only need to iterate i over [ 2, …, sqrt(N) ]
163
+
- To further improve this, notice how for a number i, we don't need to verify multiples smaller than i^2, because these numbers already been squashed in previous steps (by the other factor of said numbers)
164
164
165
-

166
-
167
-
Notice how, for i = 3, 6 has already marked in a previous step (in i = 2).
165
+

166
+
Notice how, for i = 3, 6 has already marked in a previous step (in i = 2).
168
167
169
168
```jsx
170
169
constcreateSieve= (N) => {
@@ -192,7 +191,7 @@ Factorization is the process of decomposing a number into prime numbers.
192
191
193
192
To solve this problem, we create modify the Sieve of Eratosthenes algorithm to, instead of saving either `true` or `false` in the array, it saves the smallest prime number that is a factor of i (or 0 if it's a prime number).
194
193
195
-

194
+

0 commit comments