|
6594 | 6594 |
|
6595 | 6595 |
|
6596 | 6596 |
|
6597 |
| - |
6598 |
| - |
6599 | 6597 |
|
6600 | 6598 |
|
6601 | 6599 |
|
6602 | 6600 |
|
6603 | 6601 |
|
6604 | 6602 |
|
| 6603 | + |
| 6604 | + |
| 6605 | + |
| 6606 | + |
6605 | 6607 |
|
6606 | 6608 |
|
6607 | 6609 |
|
|
6624 | 6626 |
|
6625 | 6627 |
|
6626 | 6628 |
|
| 6629 | + |
| 6630 | + |
| 6631 | + |
| 6632 | + |
6627 | 6633 |
|
6628 | 6634 |
|
6629 | 6635 |
|
|
6742 | 6748 |
|
6743 | 6749 |
|
6744 | 6750 |
|
| 6751 | + |
| 6752 | + |
| 6753 | + |
| 6754 | + |
| 6755 | + |
| 6756 | + |
| 6757 | + |
| 6758 | + |
6745 | 6759 |
|
6746 | 6760 |
|
6747 | 6761 |
|
|
6758 | 6772 | <ul class="metadata page-metadata" data-bi-name="page info" lang="en-us" dir="ltr">
|
6759 | 6773 |
|
6760 | 6774 | Last update:
|
6761 |
| - <span class="git-revision-date-localized-plugin git-revision-date-localized-plugin-date" title="April 17, 2025 18:03:29">April 17, 2025</span>  |
| 6775 | + <span class="git-revision-date-localized-plugin git-revision-date-localized-plugin-date" title="April 22, 2025 01:11:02">April 22, 2025</span>  |
6762 | 6776 |
|
6763 | 6777 | <!-- Tags -->
|
6764 | 6778 |
|
@@ -6795,7 +6809,9 @@ <h2 id="properties">Properties<a class="headerlink" href="#properties" title="Pe
|
6795 | 6809 | <li>Cassini's identity:</li>
|
6796 | 6810 | </ul>
|
6797 | 6811 | <div class="arithmatex">$$F_{n-1} F_{n+1} - F_n^2 = (-1)^n$$</div>
|
6798 |
| -<p>This can be proved by induction. A one-line proof due to Knuth comes from taking the determinant of the 2x2 matrix form below.</p> |
| 6812 | +<blockquote> |
| 6813 | +<p>This can be proved by induction. A one-line proof by Knuth comes from taking the determinant of the 2x2 matrix form below.</p> |
| 6814 | +</blockquote> |
6799 | 6815 | <ul>
|
6800 | 6816 | <li>The "addition" rule:</li>
|
6801 | 6817 | </ul>
|
@@ -6876,9 +6892,45 @@ <h3 id="fibonacci-in-linear-time">Fibonacci in linear time<a class="headerlink"
|
6876 | 6892 | </code></pre></div>
|
6877 | 6893 | <p>In this way, we obtain a linear solution, <span class="arithmatex">$O(n)$</span> time, saving all the values prior to <span class="arithmatex">$n$</span> in the sequence.</p>
|
6878 | 6894 | <h3 id="matrix-form">Matrix form<a class="headerlink" href="#matrix-form" title="Permanent link">¶</a></h3>
|
6879 |
| -<p>It is easy to prove the following relation:</p> |
6880 |
| -<div class="arithmatex">$$\begin{pmatrix} 1 & 1 \cr 1 & 0 \cr\end{pmatrix} ^ n = \begin{pmatrix} F_{n+1} & F_{n} \cr F_{n} & F_{n-1} \cr\end{pmatrix}$$</div> |
6881 |
| -<p>Thus, in order to find <span class="arithmatex">$F_n$</span> in <span class="arithmatex">$O(log n)$</span> time, we must raise the matrix to n. (See <a href="binary-exp.html">Binary exponentiation</a>)</p> |
| 6895 | +<p>To go from <span class="arithmatex">$(F_n, F_{n-1})$</span> to <span class="arithmatex">$(F_{n+1}, F_n)$</span>, we can express the linear recurrence as a 2x2 matrix multiplication:</p> |
| 6896 | +<div class="arithmatex">$$ |
| 6897 | +\begin{pmatrix} |
| 6898 | +1 & 1 \\ |
| 6899 | +1 & 0 |
| 6900 | +\end{pmatrix} |
| 6901 | +\begin{pmatrix} |
| 6902 | +F_n \\ |
| 6903 | +F_{n-1} |
| 6904 | +\end{pmatrix} |
| 6905 | += |
| 6906 | +\begin{pmatrix} |
| 6907 | +F_n + F_{n-1} \\ |
| 6908 | +F_{n} |
| 6909 | +\end{pmatrix} |
| 6910 | += |
| 6911 | +\begin{pmatrix} |
| 6912 | +F_{n+1} \\ |
| 6913 | +F_{n} |
| 6914 | +\end{pmatrix} |
| 6915 | +$$</div> |
| 6916 | +<p>This lets us treat iterating the recurrence as repeated matrix multiplication, which has nice properties. In particular,</p> |
| 6917 | +<div class="arithmatex">$$ |
| 6918 | +\begin{pmatrix} |
| 6919 | +1 & 1 \\ |
| 6920 | +1 & 0 |
| 6921 | +\end{pmatrix}^n |
| 6922 | +\begin{pmatrix} |
| 6923 | +F_1 \\ |
| 6924 | +F_0 |
| 6925 | +\end{pmatrix} |
| 6926 | += |
| 6927 | +\begin{pmatrix} |
| 6928 | +F_{n+1} \\ |
| 6929 | +F_{n} |
| 6930 | +\end{pmatrix} |
| 6931 | +$$</div> |
| 6932 | +<p>where <span class="arithmatex">$F_1 = 1, F_0 = 0$</span>.</p> |
| 6933 | +<p>Thus, in order to find <span class="arithmatex">$F_n$</span> in <span class="arithmatex">$O(\log n)$</span> time, we must raise the matrix to n. (See <a href="binary-exp.html">Binary exponentiation</a>)</p> |
6882 | 6934 | <div class="highlight"><pre><span></span><code><span class="k">struct</span><span class="w"> </span><span class="nc">matrix</span><span class="w"> </span><span class="p">{</span>
|
6883 | 6935 | <span class="w"> </span><span class="kt">long</span><span class="w"> </span><span class="kt">long</span><span class="w"> </span><span class="n">mat</span><span class="p">[</span><span class="mi">2</span><span class="p">][</span><span class="mi">2</span><span class="p">];</span>
|
6884 | 6936 | <span class="w"> </span><span class="n">matrix</span><span class="w"> </span><span class="k">friend</span><span class="w"> </span><span class="k">operator</span><span class="w"> </span><span class="o">*</span><span class="p">(</span><span class="k">const</span><span class="w"> </span><span class="n">matrix</span><span class="w"> </span><span class="o">&</span><span class="n">a</span><span class="p">,</span><span class="w"> </span><span class="k">const</span><span class="w"> </span><span class="n">matrix</span><span class="w"> </span><span class="o">&</span><span class="n">b</span><span class="p">){</span>
|
@@ -6982,7 +7034,7 @@ <h2 id="practice-problems">Practice Problems<a class="headerlink" href="#practic
|
6982 | 7034 |
|
6983 | 7035 | <ul class="metadata page-metadata" data-bi-name="page info" lang="en-us" dir="ltr">
|
6984 | 7036 | <span class="contributors-text">Contributors:</span>
|
6985 |
| - <ul class="contributors" data-bi-name="contributors"><li><a href="https://github.com/gabrielsimoes" title="gabrielsimoes" data-bi-name="contributorprofile" target="_blank">gabrielsimoes</a> (28.63%)</li><li><a href="https://github.com/jakobkogler" title="jakobkogler" data-bi-name="contributorprofile" target="_blank">jakobkogler</a> (25.31%)</li><li><a href="#" title="Carlos Javier Blanco" data-bi-name="contributorprofile" target="_blank">Carlos Javier Blanco</a> (20.33%)</li><li><a href="https://github.com/madhur4127" title="madhur4127" data-bi-name="contributorprofile" target="_blank">madhur4127</a> (7.47%)</li><li><a href="https://github.com/tcNickolas" title="tcNickolas" data-bi-name="contributorprofile" target="_blank">tcNickolas</a> (6.22%)</li><li><a href="https://github.com/adamant-pwn" title="adamant-pwn" data-bi-name="contributorprofile" target="_blank">adamant-pwn</a> (2.49%)</li><li><a href="https://github.com/Morass" title="Morass" data-bi-name="contributorprofile" target="_blank">Morass</a> (2.49%)</li><li><a href="https://github.com/md-shamim-ahmad" title="md-shamim-ahmad" data-bi-name="contributorprofile" target="_blank">md-shamim-ahmad</a> (2.07%)</li><li><a href="https://github.com/boxlesscat" title="boxlesscat" data-bi-name="contributorprofile" target="_blank">boxlesscat</a> (1.24%)</li><li><a href="https://github.com/yeetholmes619" title="yeetholmes619" data-bi-name="contributorprofile" target="_blank">yeetholmes619</a> (1.24%)</li><li><a href="https://github.com/jxu" title="jxu" data-bi-name="contributorprofile" target="_blank">jxu</a> (0.83%)</li><li><a href="https://github.com/pasthec" title="pasthec" data-bi-name="contributorprofile" target="_blank">pasthec</a> (0.83%)</li><li><a href="https://github.com/conlacda" title="conlacda" data-bi-name="contributorprofile" target="_blank">conlacda</a> (0.41%)</li><li><a href="https://github.com/ankit-kumar-dwivedi" title="ankit-kumar-dwivedi" data-bi-name="contributorprofile" target="_blank">ankit-kumar-dwivedi</a> (0.41%)</li></ul> |
| 7037 | + <ul class="contributors" data-bi-name="contributors"><li><a href="https://github.com/gabrielsimoes" title="gabrielsimoes" data-bi-name="contributorprofile" target="_blank">gabrielsimoes</a> (24.64%)</li><li><a href="https://github.com/jakobkogler" title="jakobkogler" data-bi-name="contributorprofile" target="_blank">jakobkogler</a> (21.43%)</li><li><a href="#" title="Carlos Javier Blanco" data-bi-name="contributorprofile" target="_blank">Carlos Javier Blanco</a> (16.43%)</li><li><a href="https://github.com/jxu" title="jxu" data-bi-name="contributorprofile" target="_blank">jxu</a> (15.36%)</li><li><a href="https://github.com/madhur4127" title="madhur4127" data-bi-name="contributorprofile" target="_blank">madhur4127</a> (6.43%)</li><li><a href="https://github.com/tcNickolas" title="tcNickolas" data-bi-name="contributorprofile" target="_blank">tcNickolas</a> (5.36%)</li><li><a href="https://github.com/adamant-pwn" title="adamant-pwn" data-bi-name="contributorprofile" target="_blank">adamant-pwn</a> (2.5%)</li><li><a href="https://github.com/Morass" title="Morass" data-bi-name="contributorprofile" target="_blank">Morass</a> (2.14%)</li><li><a href="https://github.com/md-shamim-ahmad" title="md-shamim-ahmad" data-bi-name="contributorprofile" target="_blank">md-shamim-ahmad</a> (1.79%)</li><li><a href="https://github.com/boxlesscat" title="boxlesscat" data-bi-name="contributorprofile" target="_blank">boxlesscat</a> (1.07%)</li><li><a href="https://github.com/yeetholmes619" title="yeetholmes619" data-bi-name="contributorprofile" target="_blank">yeetholmes619</a> (1.07%)</li><li><a href="https://github.com/pasthec" title="pasthec" data-bi-name="contributorprofile" target="_blank">pasthec</a> (0.71%)</li><li><a href="https://github.com/mhayter" title="mhayter" data-bi-name="contributorprofile" target="_blank">mhayter</a> (0.36%)</li><li><a href="https://github.com/conlacda" title="conlacda" data-bi-name="contributorprofile" target="_blank">conlacda</a> (0.36%)</li><li><a href="https://github.com/ankit-kumar-dwivedi" title="ankit-kumar-dwivedi" data-bi-name="contributorprofile" target="_blank">ankit-kumar-dwivedi</a> (0.36%)</li></ul> |
6986 | 7038 | </ul>
|
6987 | 7039 |
|
6988 | 7040 | </article>
|
|
0 commit comments