4
\documentclass[12pt,pstricks,border=5pt]{standalone}

\usepackage{pst-func,pst-calculate}
\begin{document}
\begin{pspicture}(-1,-1)(1,1)
\def\q{\pscalculate{1/sqrt(5)}}
\def\a{\pscalculate{(1+sqrt(5))/2}}
\foreach \i in {0,1,...,10}{\pscalculate{\q*((\a)^(\i)-(1-\a)^(\i))},}
\end{pspicture}

\end{document}

I want to generate a list of Fibonacci sequence but without trailing zeros.

enter image description here

How to remove the trailing zeros?

Display Name
  • 46,933
  • 1
    People visiting here: there are a number of other pages on this site mentioning Fibonacci numbers, in particular https://tex.stackexchange.com/questions/51414/fibonacci-numbers –  Jan 05 '19 at 11:28

4 Answers4

3

\psCalculate (with an uppercase C) can accept some settings.

\documentclass[12pt,border=5pt]{standalone}
\usepackage{pst-calculate,pgffor}
\def\q{\pscalculate{1/sqrt(5)}}
\def\a{\pscalculate{(1+sqrt(5))/2}}

\begin{document}
$0\foreach \i in {1,...,10}{,\psCalculate[round-mode=places,round-precision=0]{\q*((\a)^(\i)-(1-\a)^(\i))}}$
\end{document}

enter image description here

Display Name
  • 46,933
  • due to rounding to nearest integer you can drop the second term very quickly (did not check starting with which \i –  Jan 04 '19 at 21:54
  • actually already with \i = 1 you can drop second term... ! –  Jan 04 '19 at 21:56
  • surprising that \pscalculate uses siunitx as dependency. –  Jan 04 '19 at 21:58
  • I can't resist to remark that, at the very moment you load pgffor, you do not need any of the other hocus pocus (sorry for the wording!): `\documentclass[12pt,border=5pt]{standalone} \usepackage{pgffor} \pgfmathsetmacro\q{1/sqrt(5)} \pgfmathsetmacro\a{(1+sqrt(5))/2}

    \begin{document} $0\foreach \i in {1,...,10}{,\pgfmathparse{int(round(\q*((\a)^(\i)-(1-\a)^(\i))))}\pgfmathresult}$ \end{document}`

    –  Jan 04 '19 at 22:00
  • you can not go beyond \i=68 as maximal value. The error apparently comes from siunitx, but at same time it is at value 69 where Fibonacci value would need more than 16 decimal digits, is l3fp doing the computations underneath? –  Jan 04 '19 at 22:03
  • @marmot my remark on the formula applies you can drop the (1-\a)^(\i) it serves nothing. But actually applying Fibonacci iteration iwth integers is much more efficient. –  Jan 04 '19 at 22:08
  • answering my question, removing the rounding done by siunitx one can go further but already at \i = 69 the precision is too bad and rounding to nearest integer would give erroneous result (117669030460994.7 whereas the real Fibonacci is 117669030460994). –  Jan 04 '19 at 22:12
  • @jfbu I agree with all this. My remark was only for the OP and on the fact that, if you load pgffor, you do not need any other package. (And you probably know much better than me the nice example on p. 640 of the pgfmanual in which the Fibonacci numbers are computed recursively, and only with integers, i.e. no rounding and so on.) –  Jan 04 '19 at 22:13
  • @marmot I surely do not know better than you the pgfmanual :), however I know the background of page 2 of xint.pdf ;-). –  Jan 04 '19 at 22:14
  • @jfbu You have a clear advantage, then. ;-) –  Jan 04 '19 at 22:15
  • It is time to solve this puzzle ... – Display Name Jan 04 '19 at 22:22
3

Fibonacci with simple LaTeX commands:

\documentclass{article}
\usepackage{ifthen}
\newcounter{fiba}\newcounter{fibb}\newcounter{fibc}\newcounter{fibrun}
\newcommand\fib[1]{%
  \init\whiledo{\thefibrun<#1}{\step\stepcounter{fibrun}}\thefiba}
\newcommand\init{%
  \setcounter{fiba}{1}\setcounter{fibb}{1}\setcounter{fibrun}{0}}
\newcommand\step{\add \rotate}
\newcommand\add{\setcounter{fibc}{\thefiba}\addtocounter{fibc}{\thefibb}}
\newcommand\rotate{\setcounter{fiba}{\thefibb}\setcounter{fibb}{\thefibc}}

\begin{document}
  \newcounter{i} \newcounter{en} \setcounter{en}{20}
  the first \theen\ Fibonacci numbers:\\
  \whiledo{\thei < \theen}{$\fib{\thei}$ \stepcounter{i}}
\end{document}

enter image description here

and the same with running lualatex:

\documentclass[12pt,a4paper]{article}
\usepackage{luacode}
\begin{luacode*}
function printFib(n)
  for i=1,n do tex.print(Fibonacci(i).." ") end
end
function Fibonacci(n)
  local function inner(m)
    if m < 2 then return m end
    return inner(m-1) + inner(m-2)
  end
  return inner(n)
end
\end{luacode*}

\begin{document}
\noindent\directlua{printFib(35)}
\end{document}

enter image description here

  • I am curious about the Lua. Are the inner values kept in memory? else recursive definition like return inner(m-1) + inner(m-2) is exponentially inefficient... –  Jan 05 '19 at 21:55
  • It is under macros/Latex/contrib/ –  Jan 26 '19 at 10:44
2

mwe

Someone should create a rlatex compiler at once, but meanwhile, I will settle with the knitr preprocess. Here the .Rnw file:

\documentclass[12pt,border=1cm]{standalone}
\begin{document}
<<Fib,echo=F>>=
Fibonacci <- function(n) {
    x <- c(0,1)
    while (length(x) < n) {nth <- length(x)
        new <- x[nth] + x[nth-1]
        x <- c(x,new)}
    return(x)}
@
\Sexpr{combine_words(Fibonacci(11))}.
\end{document}
Fran
  • 80,769
1

Just for fun. Without rounding and without pscalculate. The example is literally copied from p. 640 of the pgfmanual. I confirm the first ten numbers of this answer (but there are rumors some mathematician called Fibonacci had them even a bit earlier just kidding! ;-).

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{math}
\begin{document}
\tikzmath{
% Adapted from http://www.cs.northwestern.edu/academics/courses/110/html/fib_rec.html 
function fibonacci(\n) {
    if \n == 0 then {
      return 0;
    } else {
       return fibonacci2(\n, 0, 1);
}; };
  function fibonacci2(\n, \p, \q) {
    if \n == 1 then {
      return \q;
    } else {
      return fibonacci2(\n-1, \q, \p+\q);
    };
  };
  int \f, \i;
  for \i in {0,1,...,20}{
    \f = fibonacci(\i);
    print {\f, };
  };
}
\end{document}

enter image description here