# How can I calculate geometric sequence in LaTeX?

I am trying to make my own command to calculate geometric sequence in LaTeX example: 10*4^(n+1) I've tried couple of things similar to this but non of them seemed to work:

\newcommand{\GeometricSequence} [1] {\the\numexpr 10*4^(#1+1) \relax \\}


So if type command (shown below) it should "print" 3360.

\GeometricSequence {3}

• Is lualatex welcome ? – Tarass Mar 21 '18 at 15:10
• Shouldn't it be 2560? – Phelype Oleinik Mar 21 '18 at 15:14
• Sadly I have to do this in LaTeX :/ No the 3360 is correct. 10*4^(1+1)+10*4^(2+1)+10*4^(3+1)=3360 – FatTommy Mar 21 '18 at 15:15
• Oh, it's the sum evaluated at each number... – Phelype Oleinik Mar 21 '18 at 15:21
• Perhaps clarify what the maximum index required is, and how many places results must be accurate to. For example, if we need full accuracy and large indices then a 'big integer' approach will be needed (all doable). – Joseph Wright Mar 21 '18 at 16:53

Up to 24 because of floating point precision limitation.

\documentclass{article}

\usepackage{tikz,xfp}

\newcommand{\GeometricSequence}[1]{%
\def\Tot{0}%
\def\Som{%
$g(#1)=\foreach \n in {1,...,#1} {\xdef\Tot{\Tot+\fpeval{10*4^(\n+1)}}% \ifnum\n>1+\fi 10\times4^{\fpeval{\n+1}}}=\fpeval{\Tot}$}%
\Som
}

\pagestyle{empty}
\begin{document}

\GeometricSequence{12}

\end{document}

• about your g(120) I think it should be printed as a floating point number, because the 58 trailing zeros have no significance. – user4686 Mar 21 '18 at 22:05
• xfp implements 16 decimal digits of precision, the only problem is that printing a number computed in floating point style as an integer with trailing zeros is a bit misleading, that's all. – user4686 Mar 21 '18 at 22:27
• Ok. I have my answer. Xint could help. – Tarass Mar 21 '18 at 22:29
• @jfbu I've corrected my answer. Thank you for your remark and explanations. – Tarass Mar 22 '18 at 6:47
• I see, but your solution is perfectly fine even for bigger argument, simply the point is that it can find only the first 16 digits of the exact result, and this why I suggested keeping if possibly the floating point notation, but apparently \fpeval{\Tot} produces the trailing zeros rather than floating point notation... – user4686 Mar 22 '18 at 7:17

The eagerly awaited unavoidable necessary xint solution.

\documentclass[12pt]{article}
\usepackage{upquote}
\usepackage{xintexpr}

\xintdefiifunc g(N) := 10 * +(4^[1+1..1+N]);

\begin{document}
\begin{verbatim}
\xintdefiifunc g(N) := 10 * +(4^[1+1..1+N]);
\end{verbatim}
$g(12) = \sum_{n=1}^{12}10\cdot 4^{1+n} = \xinttheiiexpr g(12)\relax$
For a one-shot calculation there is a more agreeable syntax than the one we
used above. Of course it gives the same
result.
\begin{verbatim}
\end{verbatim}
$g(12) = \sum_{n=1}^{12}10\cdot 4^{1+n} = \xinttheiiexpr add(10*4^(1+n), n=1..12)\relax$
It is possible to abstract this syntax into a macro-like function definition:
\begin{verbatim}
\end{verbatim}
It gives again same result
$g(12) = \sum_{n=1}^{12}10\cdot 4^{1+n} = \xinttheiiexpr G(12)\relax$ But
the \verb|G| does again all the needed parsing which has already been encoded
into the faster \verb|g| function. The problem is that we can't currently use
\verb|a..b| syntax in \verb|\xintdefiifunc| definitions when \texttt{a} or
\texttt{b} are among the function parameters. We can always use
\verb|\xintNewFunction| as shown above.
\thispagestyle{empty}
\end{document}


Still one more syntax:

$g(120) = \xinttheiiexpr 10 * +(rseq(16; 4*@, i=2..120))\relax$


This could be faster than computing all powers from scratch as it proceeds iteratively... while we are at it we would also use the exact mathematical expression for the sum, of course this is still faster.

Using the LaTeX3 FPU we could do for example

\documentclass{article}
\usepackage{xparse,expl3}
\ExplSyntaxOn
\NewExpandableDocumentCommand{\GeometricSequence}{m}{%
\fp_eval:n { 0 \int_step_function:nnnN {1} {1} {#1} \__ft_geomseq:n }
}
\cs_new:Npn \__ft_geomseq:n #1
{ + 10*4^(#1 + 1) }
\ExplSyntaxOff
\begin{document}

\GeometricSequence{3}

\end{document}


An alternative approach using integer mathematics only and allowing arbitrary output size (index limit here is that of TeX):

\documentclass{article}
\usepackage{expl3,bigintcalc}
\ExplSyntaxOn
\cs_new_eq:NN \intstep \int_step_function:nnnN
\ExplSyntaxOff
\newcommand*{\GeometricSequence}[1]{%
\intstep{1}{1}{#1}\GeometricSequenceAux\GeometricSequenceEnd{0}%
}
\newcommand*{\GeometricSequenceAux}[1]{}
\def\GeometricSequenceAux#1#2\GeometricSequenceEnd#3{%
#2%
\GeometricSequenceEnd
{%
}%
}
\newcommand*\GeometricSequenceEnd[1]{#1}
\begin{document}
\GeometricSequence{3}

\end{document}


Here's a LuaLaTeX-based solution. It sets up two LaTeX user macros: \GeoSeq{p}, to calculate the value of the function for a given integer p, and \GeoTable{n}, to create the contents of a tabular environment for the first n integers.

\documentclass{article}
\usepackage{luacode} % for 'luacode' environment
\begin{luacode}
function GeoSeq ( p )
local Total = 0
for i = 1 , p do
Total = Total + 4^(1+i)
end
return ( 10 * Total )
end
function GeoTable ( n )
for i = 1 , n do
tex.sprint ( i .. "&" .. GeoSeq(i) .. "\\\\" )
end
end
\end{luacode}

\newcommand\GeoSeq[1]{\directlua{tex.sprint(GeoSeq(#1))}}
\newcommand\GeoTable[1]{\directlua{GeoTable(#1)}}

\begin{document}