fibonacci series mod a number - MathOverflow most recent 30 from http://mathoverflow.net2013-06-20T00:35:55Zhttp://mathoverflow.net/feeds/question/40816http://www.creativecommons.org/licenses/by-nc/2.5/rdfhttp://mathoverflow.net/questions/40816/fibonacci-series-mod-a-numberfibonacci series mod a numberunknown (google)2010-10-02T05:18:35Z2010-11-07T17:19:14Z
<p>I'm trying to write a program with an input of numbers $n$ and $k$ (where $n<10^{1000}$ and $k<10^9$), where I compute fib[n] % k.
What is a good FAST way of computing this? </p>
<p>I realize that the resulting series is periodic, just not sure how to find it efficiently.</p>
http://mathoverflow.net/questions/40816/fibonacci-series-mod-a-number/40818#40818Answer by Robin Chapman for fibonacci series mod a numberRobin Chapman2010-10-02T06:51:18Z2010-10-02T06:51:18Z<p>This is really just an expansion of Gerhard's comment. One has the matrix formula
$$\begin{pmatrix}
1&1\\
1&0
\end{pmatrix}^n=
\begin{pmatrix}
F_{n+1}&F_n\\
F_n&F_{n-1}
\end{pmatrix}
$$
so the problem reduces to computing $A^n$ modulo $k$ where
$$A=\begin{pmatrix}
1&1\\
1&0
\end{pmatrix}.$$
This can be done by the <a href="http://en.wikipedia.org/wiki/Exponentiation_by_squaring" rel="nofollow">repeated squaring</a> method often used in
<a href="http://en.wikipedia.org/wiki/Modular_exponentiation" rel="nofollow">modular exponentiation</a>. The idea is to compute $A^n$ recursively
either as $(A^m)^2$ or $A(A^m)^2$ according to whether $n=2m$ or $n=2m+1$.</p>
http://mathoverflow.net/questions/40816/fibonacci-series-mod-a-number/45183#45183Answer by Andreas Rüdinger for fibonacci series mod a numberAndreas Rüdinger2010-11-07T17:19:14Z2010-11-07T17:19:14Z<p>Perhaps Elsenhans, Jahnel, "The Fibonacci sequence modulo $p^2$ –
An investigation by computer for $p < 10^{14}$" <a href="http://www.uni-math.gwdg.de/tschinkel/gauss/Fibon.pdf" rel="nofollow">http://www.uni-math.gwdg.de/tschinkel/gauss/Fibon.pdf</a> will be interesting for you. There are sections about the algorithm. </p>