![如何在 LaTeX 中计算几何序列?](https://linux22.com/image/372672/%E5%A6%82%E4%BD%95%E5%9C%A8%20LaTeX%20%E4%B8%AD%E8%AE%A1%E7%AE%97%E5%87%A0%E4%BD%95%E5%BA%8F%E5%88%97%EF%BC%9F.png)
我正在尝试使用自己的命令来计算 LaTeX 中的几何序列,例如:10*4^(n+1) 我尝试过一些类似的东西,但似乎都不起作用:
\newcommand{\GeometricSequence} [1] {\the\numexpr 10*4^(#1+1) \relax \\}
因此,如果输入命令(如下所示),它应该“打印”3360。
\GeometricSequence {3}
答案1
由于浮点精度限制,最多为 24。
\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}
答案2
人们热切期待的不可避免的必要xint
解决方案。
\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}
还有一个语法:
$g(120) = \xinttheiiexpr 10 * `+`(rseq(16; 4*@, i=2..120))\relax$
这可能比从头开始计算所有幂更快,因为它会迭代进行...当我们这样做时,我们也会使用精确的数学表达式来计算总和,当然这仍然更快。
答案3
使用 LaTeX3 FPU 我们可以这样做
\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}
另一种方法仅使用整数数学并允许任意输出大小(此处的索引限制是 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}
答案4
这是一个基于 LuaLaTeX 的解决方案。它设置了两个 LaTeX 用户宏:\GeoSeq{p}
,用于计算给定整数的函数值p
,以及,用于为第一个整数\GeoTable{n}
创建环境的内容。tabular
n
\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}
\GeoSeq{3} \qquad
\begin{tabular}[t]{@{}lr@{}}
\GeoTable{10} % print 10 rows
\end{tabular}
\end{document}