Plot a factorial function

Hello I want to plot a graph for of n! since I'm working with O(n!) time complexity. How can I make this look smooth? Preferably I want to show the marks. I tried doing plotting the first 10 factorials. If there is better way to display operations per element that would be helpful too.

\begin{tikzpicture}
\begin{axis}[
title={O(n!) Scaling data input},
xlabel={Elements},
ylabel={Operations},
xmin=0, xmax=25,
ymin=0, ymax=3628800,
xtick={0,1,2,3,4,5,6,7,8,8,10},
ytick={0,1,2,6,24,120,720,5040,40320,632880,3628800},
legend pos=north west,
ymajorgrids=true,
grid style=dashed,
]
color=blue,
mark=square,
]
coordinates {
(0,1)(1,1)(2,2)(3,6)(4,24)(5,120)(6,720)(7,5040)(8,40320)(9,632880)(10,3628800)
};
\legend{O(n!)}

\end{axis}
\end{tikzpicture}


• Could you use a semilog? Commented May 23, 2017 at 15:30
• Note that a factorial is a special case of the Gamma function, i.e. $\Gamma(n+1)=n!$, but the Gamma function can also do fractions. So just plot the Gamma function.
– JPi
Commented May 23, 2017 at 15:43
• So a Gamma function as a semilog with log on y axis? Commented May 23, 2017 at 15:46
• Are you plotting something where the result makes sense for non-integer n? Otherwise, it doesn't make sense to join the points.
– cfr
Commented May 23, 2017 at 17:42
• @J.Kirk.: sure.
– JPi
Commented May 23, 2017 at 18:18

Perhaps you'd like to use semilogyaxis, for example:

\documentclass{standalone}
\usepackage{pgfplots}

\begin{document}
\begin{tikzpicture}
\begin{semilogyaxis}[
title={O(n!) Scaling data input},
xlabel={Elements},
ylabel={Operations},
xmin=0, xmax=10,
ymin=0, ymax=3628800,
xtick={0,1,2,3,4,5,6,7,8,8,10},
ytick={0,1,2,6,24,120,720,5040,40320,632880,3628800},
legend pos=north west,
ymajorgrids=true,
grid style=dashed,
]
color=blue,
only marks,
mark=square,
]
coordinates {
(0,1)(1,1)(2,2)(3,6)(4,24)(5,120)(6,720)(7,5040)(8,40320)(9,632880)(10,3628800)
};
\legend{O(n!)}
\end{semilogyaxis}
\end{tikzpicture}
\end{document}


As mentioned in the other answer you might want to use a semilogarithmic axis. Using the FFI of LuaJITTeX (and LuaTeX ≥ 1.0.3), you can also plot the Gamma function alongside, as you wanted to have a "smooth factorial function".

Also I corrected your typo in 9! (it's 362880 instead of 632880).

\documentclass{article}

\usepackage{pgfplots}

\directlua{
ffi=require("ffi")
ffi.cdef[[
double tgamma(double x);
]]
}

\pgfmathdeclarefunction{Gamma}{1}{%
\edef\pgfmathresult{%
\directlua{tex.print(ffi.C.tgamma(\pgfmathfloatvalueof{#1}))}%
}%
}

\begin{document}

\begin{tikzpicture}
\begin{semilogyaxis}[
title={$O(n!)$ Scaling data input},
xlabel={Elements},
ylabel={Operations},
domain=0:10,
ytick={0,1,2,6,24,120,720,5040,40320,362880,3628800},
log ticks with fixed point,
legend pos=north west,
ymajorgrids=true,
grid style=dashed,
]
\addplot+[mark=square,only marks] coordinates {
(0,1) (1,1) (2,2) (3,6) (4,24) (5,120) (6,720) (7,5040)
(8,40320) (9,362880) (10,3628800)
};
\addlegendentry{$O(n!)$}

\addlegendentry{$\Gamma(n+1)$}

\end{semilogyaxis}
\end{tikzpicture}

\end{document}


• Would it be better to use straight lines to connect the data? The parabola (?) between the first two data points now suggests a decrease followed by an increase. Commented May 27, 2017 at 21:32
• @Karlo No, that is correct. This is what the Gamma function looks like. Commented May 28, 2017 at 3:03

As cmhughes already stated in his answer, the key is to use a logarithmic y axis.

This answer is just to show how else you could have created your graph by calculating the values instead of giving them explicitly. In contrast to the answer of Henri Menke here the values are only calculated for the integer x values, but therefor it can be done by LaTeX itself, so without the need of using FFI of LuaJITTeX.

% used PGFPlots v1.14
\documentclass[border=5pt]{standalone}
\usepackage{pgfplots}
% use this compat level to use the advanced positioning features of the axis labels
\pgfplotsset{compat=1.3}
\begin{document}
\begin{tikzpicture}
% use log y-axis
\begin{semilogyaxis}[
title={$O(n!)$ Scaling data input},
xlabel={Elements},
ylabel={Operations},
% so you don't have to state the values manually
% (the data of the first given plot (only) are used)
xtick=data,
ytick=data,
% you can also use ...
enlargelimits=false,
%        % ... instead of giving the limits explicitly
%        xmin=0, xmax=10,
%        ymin=0, ymax=3628800,
legend pos=north west,
legend cell align=left,
ymajorgrids=true,
grid style=dashed,
log ticks with fixed point,
only marks,
]
% automatic calculation of $n!$
% (I placed it before the "manual" variant so that the right yticks are shown)
red,
mark=*,
mark size=1.5pt,
% state the domain where the values should be calculated
domain=0:10,
% state the number of samples that should be calculated
samples=11,
] {x!};
% typo at $9!$ (switched first two digits)
\addplot [color=blue,mark=square] coordinates {
(0,1)(1,1)(2,2)(3,6)(4,24)(5,120)(6,720)(7,5040)(8,40320)(9,632880)(10,3628800)
};

\legend{
$O(n!)$ (calculated),
$O(n!)$ (by hand),
}
\end{semilogyaxis}
\end{tikzpicture}
\end{document}