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: main/algebra/factorization.html
+2-1Lines changed: 2 additions & 1 deletion
Original file line number
Diff line number
Diff line change
@@ -6919,7 +6919,8 @@ <h2 id="pollards-rho-algorithm">Pollard's rho algorithm<a class="headerlink" hre
6919
6919
If <spanclass="arithmatex">$p$</span> is smaller than <spanclass="arithmatex">$\sqrt{n}$</span>, the repetition will likely start in <spanclass="arithmatex">$O(\sqrt[4]{n})$</span>.</p>
6920
6920
<p>Here is a visualization of such a sequence <spanclass="arithmatex">$\{x_i \bmod p\}$</span> with <spanclass="arithmatex">$n = 2206637$</span>, <spanclass="arithmatex">$p = 317$</span>, <spanclass="arithmatex">$x_0 = 2$</span> and <spanclass="arithmatex">$f(x) = x^2 + 1$</span>.
6921
6921
From the form of the sequence you can see very clearly why the algorithm is called Pollard's <spanclass="arithmatex">$\rho$</span> algorithm.</p>
How can we exploit the properties of the sequence <spanclass="arithmatex">$\{x_i \bmod p\}$</span> to our advantage without even knowing the number <spanclass="arithmatex">$p$</span> itself?</p>
Copy file name to clipboardExpand all lines: main/algebra/sieve-of-eratosthenes.html
+2-1Lines changed: 2 additions & 1 deletion
Original file line number
Diff line number
Diff line change
@@ -6816,7 +6816,8 @@ <h1 id="sieve-of-eratosthenes">Sieve of Eratosthenes<a class="headerlink" href="
6816
6816
The next unmarked number is 5, which is the next prime number, and we mark all proper multiples of it.
6817
6817
And we continue this procedure until we have processed all numbers in the row.</p>
6818
6818
<p>In the following image you can see a visualization of the algorithm for computing all prime numbers in the range <spanclass="arithmatex">$[1; 16]$</span>. It can be seen, that quite often we mark numbers as composite multiple times.</p>
6819
-
<p><center><imgalt="Sieve of Eratosthenes" src="sieve_eratosthenes.png" /></center></p>
6819
+
<center></center>
6820
+
6820
6821
<p>The idea behind is this:
6821
6822
A number is prime, if none of the smaller prime numbers divides it.
6822
6823
Since we iterate over the prime numbers in order, we already marked all numbers, which are divisible by at least one of the prime numbers, as divisible.
<h3id="finding-sum-in-one-dimensional-array">Finding sum in one-dimensional array<aclass="headerlink" href="#finding-sum-in-one-dimensional-array" title="Permanent link">¶</a></h3>
7035
7036
<p>Here we present an implementation of the Fenwick tree for sum queries and single updates.</p>
<p>More specifically, treap is a data structure that stores pairs <spanclass="arithmatex">$(X, Y)$</span> in a binary tree in such a way that it is a binary search tree by <spanclass="arithmatex">$X$</span> and a binary heap by <spanclass="arithmatex">$Y$</span>.
6800
6800
If some node of the tree contains values <spanclass="arithmatex">$(X_0, Y_0)$</span>, all nodes in the left subtree have <spanclass="arithmatex">$X \leq X_0$</span>, all nodes in the right subtree have <spanclass="arithmatex">$X_0 \leq X$</span>, and all nodes in both left and right subtrees have <spanclass="arithmatex">$Y \leq Y_0$</span>.</p>
6801
6801
<p>A treap is also often referred to as a "cartesian tree", as it is easy to embed it in a Cartesian plane:</p>
<p>Treaps have been proposed by Raimund Siedel and Cecilia Aragon in 1989.</p>
6806
6807
<h2id="advantages-of-such-data-organisation">Advantages of such data organisation<aclass="headerlink" href="#advantages-of-such-data-organisation" title="Permanent link">¶</a></h2>
6807
6808
<p>In such implementation, <spanclass="arithmatex">$X$</span> values are the keys (and at same time the values stored in the treap), and <spanclass="arithmatex">$Y$</span> values are called <strong>priorities</strong>. Without priorities, the treap would be a regular binary search tree by <spanclass="arithmatex">$X$</span>, and one set of <spanclass="arithmatex">$X$</span> values could correspond to a lot of different trees, some of them degenerate (for example, in the form of a linked list), and therefore extremely slow (the main operations would have <spanclass="arithmatex">$O(N)$</span> complexity).</p>
<p>In terms of implementation, each node contains <spanclass="arithmatex">$X$</span>, <spanclass="arithmatex">$Y$</span> and pointers to the left (<spanclass="arithmatex">$L$</span>) and right (<spanclass="arithmatex">$R$</span>) children.</p>
6828
6829
<p>We will implement all the required operations using just two auxiliary operations: Split and Merge.</p>
<p><strong>Split (<spanclass="arithmatex">$T$</span>, <spanclass="arithmatex">$X$</span>)</strong> separates tree <spanclass="arithmatex">$T$</span> in 2 subtrees <spanclass="arithmatex">$L$</span> and <spanclass="arithmatex">$R$</span> trees (which are the return values of split) so that <spanclass="arithmatex">$L$</span> contains all elements with key <spanclass="arithmatex">$X_L \le X$</span>, and <spanclass="arithmatex">$R$</span> contains all elements with key <spanclass="arithmatex">$X_R > X$</span>. This operation has <spanclass="arithmatex">$O (\log N)$</span> complexity and is implemented using a clean recursion:</p>
6834
6836
<ol>
6835
6837
<li>If the value of the root node (R) is <spanclass="arithmatex">$\le X$</span>, then <code>L</code> would at least consist of <code>R->L</code> and <code>R</code>. We then call split on <code>R->R</code>, and note its split result as <code>L'</code> and <code>R'</code>. Finally, <code>L</code> would also contain <code>L'</code>, whereas <code>R = R'</code>.</li>
<p><strong>Merge (<spanclass="arithmatex">$T_1$</span>, <spanclass="arithmatex">$T_2$</span>)</strong> combines two subtrees <spanclass="arithmatex">$T_1$</span> and <spanclass="arithmatex">$T_2$</span> and returns the new tree. This operation also has <spanclass="arithmatex">$O (\log N)$</span> complexity. It works under the assumption that <spanclass="arithmatex">$T_1$</span> and <spanclass="arithmatex">$T_2$</span> are ordered (all keys <spanclass="arithmatex">$X$</span> in <spanclass="arithmatex">$T_1$</span> are smaller than keys in <spanclass="arithmatex">$T_2$</span>). Thus, we need to combine these trees without violating the order of priorities <spanclass="arithmatex">$Y$</span>. To do this, we choose as the root the tree which has higher priority <spanclass="arithmatex">$Y$</span> in the root node, and recursively call Merge for the other tree and the corresponding subtree of the selected root node.</p>
<p>Now implementation of <strong>Insert (<spanclass="arithmatex">$X$</span>, <spanclass="arithmatex">$Y$</span>)</strong> becomes obvious. First we descend in the tree (as in a regular binary search tree by X), and stop at the first node in which the priority value is less than <spanclass="arithmatex">$Y$</span>. We have found the place where we will insert the new element. Next, we call <strong>Split (T, X)</strong> on the subtree starting at the found node, and use returned subtrees <spanclass="arithmatex">$L$</span> and <spanclass="arithmatex">$R$</span> as left and right children of the new node.</p>
6854
6858
<p>Alternatively, insert can be done by splitting the initial treap on <spanclass="arithmatex">$X$</span> and doing <spanclass="arithmatex">$2$</span> merges with the new node (see the picture).</p>
<p>Implementation of <strong>Erase (<spanclass="arithmatex">$X$</span>)</strong> is also clear. First we descend in the tree (as in a regular binary search tree by <spanclass="arithmatex">$X$</span>), looking for the element we want to delete. Once the node is found, we call <strong>Merge</strong> on it children and put the return value of the operation in the place of the element we're deleting.</p>
6860
6865
<p>Alternatively, we can factor out the subtree holding <spanclass="arithmatex">$X$</span> with <spanclass="arithmatex">$2$</span> split operations and merge the remaining treaps (see the picture).</p>
0 commit comments