How can I calculate geometric sequence in LaTeX?
The eagerly awaited unavoidable necessary xint
solution.
\documentclass[12pt]{article}
\usepackage{upquote}
\usepackage{xintexpr}
\xintdefiifunc g(N) := 10 * `+`(4^[1+1..1+N]);
\xintNewFunction{G}[1]{10 * add(4^(1+n), n=1..#1)}
\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}
\xinttheiiexpr add(10*4^(1+n), n=1..12)\relax
\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}
\xintNewFunction{G}[1]{10 * add(4^(1+n), n=1..#1)}
\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
{%
\bigintcalcAdd{#3}
{\bigintcalcMul{10}{\bigintcalcPow{4}{\bigintcalcAdd{1}{#1}}}}%
}%
}
\newcommand*\GeometricSequenceEnd[1]{#1}
\begin{document}
\GeometricSequence{3}
\end{document}
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}