An implementation in LaTeX3:
\documentclass{article}
\usepackage{xparse}
\ExplSyntaxOn
\cs_new:Npn \fibo #1 { \fibo_recurrence:nnnn{0}{1}{0}{#1} }
\cs_new:Npn \fibo_recurrence:nnnn #1 #2 #3 #4
{
\int_compare:nTF { #1 = #4 }
{ #3 }
{
#3 ~ \fibo_recurrence:ffnn
{ \int_eval:n {#1+1} }
{ \int_eval:n {#2+#3} }
{ #2 }
{ #4 }
}
}
\cs_generate_variant:Nn \fibo_recurrence:nnnn { ffnn }
\ExplSyntaxOff
\begin{document}
\fibo{0}
\fibo{1}
\fibo{2}
\fibo{3}
\fibo{7}
\fibo{45}
\end{document}
Notice that this is completely expandable. This prints
0
0 1
0 1 1
0 1 1 2
0 1 1 2 3 5 8 13
0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597 2584 4181 6765 10946
17711 28657 46368 75025 121393 196418 317811 514229 832040 1346269 2178309
3524578 5702887 9227465 14930352 24157817 39088169 63245986 102334155
165580141 267914296 433494437 701408733 1134903170
but with \printfibonacci{46} we get Arithmetic overflow.
One can overcome the limitation with the bigintcalc package:
\documentclass{article}
\usepackage{xparse,bigintcalc}
\ExplSyntaxOn
\cs_new:Npn \fibo #1 { \fibo_recurrence:nnnn{0}{1}{0}{#1} }
\cs_new:Npn \fibo_recurrence:nnnn #1 #2 #3 #4
{
\int_compare:nTF { #1 = #4 }
{ $f\sb{#1}=#3$ }
{
$f\sb{#1}=#3$, ~ \fibo_recurrence:ffnn
{ \int_eval:n {#1+1} }
{ \bigintcalcAdd{#2}{#3} }
{ #2 }
{ #4 }
}
}
\cs_generate_variant:Nn \fibo_recurrence:nnnn { ffnn }
\ExplSyntaxOff
\begin{document}
\raggedright
\fibo{100}
\end{document}
will produce (and shows also how to print other information)

With a little twist the macro can build every degree 2 recurrent sequence (with integer coefficients), that is, of the form
an+2 = pan+1 + qan
\usepackage{xparse}
\ExplSyntaxOn
\cs_new:Npn \fibo #1 { \rec_recurrence:nnnnnn {0}{1}{0}{#1}{1}{1} }
\cs_new:Npn \periodic #1 { \rec_recurrence:nnnnnn {0}{0}{1}{#1}{0}{-1} }
\cs_new:Npn \rec_recurrence:nnnnnn #1 #2 #3 #4 #5 #6
{
\int_compare:nTF { #1 = #4 }
{ $#3$ }
{
$#3$ ~ \rec_recurrence:ffnnnn
{ \int_eval:n {#1+1} }
{ \int_eval:n {#5*#2+#6*#3} }
{ #2 }
{ #4 }
{ #5 }
{ #6 }
}
}
\cs_generate_variant:Nn \rec_recurrence:nnnnnn { ff }
\cs_new:Npn \fibo #1 { \rec_recurrence:nnnnnn {0}{1}{0}{#1}{1}{1} }
\cs_new:Npn \periodic #1 { \rec_recurrence:nnnnnn {0}{0}{1}{#1}{0}{-1} }
\ExplSyntaxOff
The arguments to \rec_recurrence:nnnnnn are
- the starting point
- the second term
- the first term
- the last term to compute
- the p coefficient
- the q coefficient
With \periodic{10} we get
1 0 −1 0 1 0 −1 0 1 0 −1
which is the recurrence
an+2 = 0an+1 + (-1)an
texorpdftexcommand. – Leo Liu Apr 10 '12 at 16:26\printfibonacciafter\begin{document}for LaTeX. Alternatively, use plain TeX. – Joseph Wright♦ Apr 10 '12 at 16:27tex.exeandpdftex.exe(@all: Andy has obviously a computer with Windows on it), but you seem to have usedlatex.exeorpdflatex.exe. Did you use the included editorTeXworks? – Speravir Apr 10 '12 at 17:06