答案1
我更喜欢将事物设置为列。
编辑但我把它画得太水平了。(我考虑过用琥珀金 ADF以更好地适应原始模型但我已经截图了)。
垂直
这里使用鑫鑫科技。
注意:宏的文档\xintEuclideAlgorithm
存在xintgcd
一些问题,示例使用\xintAssign
但它应该有可选参数[e]
来强制扩展。行为在\xintAssign
大约三年前发生了变化,但此处的使用并未更新。
\documentclass[a4paper]{article}
\usepackage{geometry}
\usepackage{xinttools,xintgcd}
\usepackage{array}
\newcommand\gcdtable[2]{%
\begingroup
\xintAssignArray\xintEuclideAlgorithm {#1}{#2}\to\EUC
% even indices 2, 4, 6, ... are A, B, R1, R2, ...
% odd indices 5, 7, .... are Q1, Q2, ...
% 1 -> number of steps, 3 -> gcd
%\edef\A{\EUC2}%\edef\B{\EUC4}%
\def\ROW##1{\expandafter\ROWa##1.}%
% (\xintiloopindex must be unbraced for its expansion)
%
\def\ROWa##1.{(R##1)&\EUC{2*##1+4}&\EUC{2*##1 + 5}&\EUC{2*##1+6}\\}%
%
\edef\NumberOfSteps{\EUC1}%
%
\[\begin{array}{cc>{{}\times{}}l>{{}+{}}l}
&&\multicolumn{1}{l}{Q}&\multicolumn{1}{l}{R}\\
\hline
(A)&\omit&\omit\\
(B)&\EUC{5}&\EUC{6}\\
\xintiloop [1+1]
\ROW{\xintiloopindex}%
\ifnum\xintiloopindex<\numexpr\NumberOfSteps-1\relax
\repeat
\hline
\multicolumn{4}{c}{\textrm{The last non zero remainder is $\EUC3$}}
\end{array}\]
\endgroup
}%
\begin{document}
\gcdtable{16}{6}
\gcdtable{100}{17}
\gcdtable{137205626}{12539182}
\gcdtable{123456789012345}{9876543210321}
\end{document}
水平
代码再次使用了\xintiloop
可扩展循环,因此可以直接在表格中使用。但不可否认的是,它的使用对位置非常敏感,而且非专家级 TeX 用户肯定无法轻松自定义。如果我使用不可扩展循环和计数器,而不是在这里&
使用,效果会更好,虽然这里很简洁,但绝对不容易使用。\xintFor
\xintiloop+\xintiloopindex
最后的小注释:这个使用tabular
,上面的一个是使用,array
因此需要数学模式。
\documentclass[a4paper]{article}
\usepackage{geometry}
\usepackage{xinttools,xintgcd}
\usepackage{array}
\newcommand\gcdhorizontaltable[2]{%
\begingroup
\xintAssignArray\xintEuclideAlgorithm {#1}{#2}\to\EUC
% even indices 2, 4, 6, ... are A, B, R1, R2, ...
% odd indices 5, 7, .... are Q1, Q2, ...
% first is number of steps N,
% "array" ends with QN in position 2N+3 and then RN=0 is in position 2N+4
% 1 -> number of steps, 3 -> gcd
% B=R0 in position 4 and A morally is R(-1) in position 2
%
% (\xintiloopindex must be unbraced for its expansion)
\def\QUO##1{\expandafter\QUOa##1.}%
\def\QUOa##1.{\EUC{2*(##1)+3}}%
%
% (\xintiloopindex must be unbraced for its expansion)
\def\REM##1{\expandafter\REMa##1.}%
\def\REMa##1.{\EUC{2*(##1)+4}}%
%
\edef\NumberOfSteps{\EUC1}%
%
\begin{tabular}{|l*{\NumberOfSteps}{|l}|}
\hline
% first row
\xintiloop [1+1]
&\QUO\xintiloopindex
\ifnum\xintiloopindex<\numexpr\NumberOfSteps\relax
\repeat \\\hline
% second row
\xintiloop [-1+1]
\REM\xintiloopindex &
\ifnum\xintiloopindex<\numexpr\NumberOfSteps-2\relax
\repeat \REM{\NumberOfSteps-1}\\\hline
% third row
\xintiloop [1+1]
\REM\xintiloopindex &
\ifnum\xintiloopindex<\numexpr\NumberOfSteps\relax
\repeat \\\hline
\end{tabular}
\endgroup
}%
\begin{document}
\gcdhorizontaltable{16}{6}
\bigskip
\gcdhorizontaltable{100}{17}
\bigskip
\gcdhorizontaltable{137205626}{12539182}
%\gcdhorizontaltable{123456789012345}{9876543210321}
\thispagestyle{empty}
\end{document}
输出:
答案2
\documentclass{article}
\def\zz#1#2#3{%
\edef\q{\the\numexpr#2/#3}%
\edef\r{\the\numexpr#2-(#3*(#2/#3))\relax}%
\ifnum\r<0
\edef\q{\the\numexpr\q - 1\relax}%
\edef\r{\the\numexpr#3+\r\relax}%
\fi
\begin{tabular}{@{}|r}#1\\#2\\\r\end{tabular}%
\ifnum\r=0
\begin{tabular}{@{}|r|@{}}\q\\#3\\{}\end{tabular}%
\else
\edef\tmp{\noexpand\zz{\q}{#3}{\r}}\tmp%
\fi}
\begin{document}
\zz{}{16}{6}
\end{document}
答案3
一种奇怪的expl3
表格构建方式,基本上适用于任意数量的步骤,但如果算法步骤太多,表格最终将溢出到边缘。
\documentclass{article}
\usepackage{xparse}
\ExplSyntaxOn
\cs_generate_variant:Nn \int_set:Nn {Nx}
\int_new:N \l_gcd_remainder_int
\int_new:N \l_gcd_integerpart_int
\seq_new:N \l_gcd_pair_seq
\NewDocumentCommand{\gcdcalc}{mm}{%
\group_begin:
% The Euclidean algorithm is in here
\int_set:Nn \l_tmpa_int {#2}
\int_set:Nn \l_tmpb_int {#1}
\seq_put_right:Nx \l_gcd_pair_seq {#1 ; #2 ; }
\int_do_until:nNnn {\l_tmpa_int} = {0} {
\int_set:Nx \l_gcd_remainder_int { \int_mod:nn {\l_tmpb_int } {\l_tmpa_int}}
\int_set:Nx \l_gcd_integerpart_int{ \int_div_truncate:nn {\l_tmpb_int } {\l_tmpa_int} }
\seq_put_right:Nx \l_gcd_pair_seq { \int_use:N \l_tmpa_int ; \int_use:N \l_gcd_remainder_int ; \int_use:N \l_gcd_integerpart_int}
\int_set_eq:NN \l_tmpb_int \l_tmpa_int
\int_set_eq:NN \l_tmpa_int \l_gcd_remainder_int
}
% Construction of the table by putting it all into a token variable
\tl_put_right:Nn \l_tmpa_tl { \hline }
\int_zero:N \l_tmpa_int
\seq_map_inline:Nn \l_gcd_pair_seq {%
\int_incr:N \l_tmpa_int
\seq_set_split:Nnn \l_tmpa_seq {;} {##1}
\int_compare:nNnTF {\l_tmpa_int } = {\seq_count:N \l_gcd_pair_seq} {
\tl_put_right:Nx \l_tmpa_tl { \seq_item:Nn \l_tmpa_seq {3} }
\tl_put_right:Nn \l_tmpa_tl { \tabularnewline\hline}
}{
\tl_put_right:Nx \l_tmpa_tl { \seq_item:Nn \l_tmpa_seq {3} & }
}
}
\int_zero:N \l_tmpa_int
\seq_map_inline:Nn \l_gcd_pair_seq {%
\int_incr:N \l_tmpa_int
\seq_set_split:Nnn \l_tmpa_seq {;} {##1}
\int_compare:nNnTF {\l_tmpa_int } = {\seq_count:N \l_gcd_pair_seq} {
\tl_put_right:Nx \l_tmpa_tl { \seq_item:Nn \l_tmpa_seq {1} }
\tl_put_right:Nn \l_tmpa_tl { \tabularnewline\hline}
}{
\tl_put_right:Nx \l_tmpa_tl { \seq_item:Nn \l_tmpa_seq {1} &}
}
}
\seq_set_eq:NN \l_tmpb_seq \l_gcd_pair_seq
\seq_pop_left:NN \l_tmpb_seq \l_tmpb_tl
\seq_map_inline:Nn \l_tmpb_seq {%
\seq_set_split:Nnn \l_tmpa_seq {;} {##1}
\tl_put_right:Nx \l_tmpa_tl {\seq_item:Nn \l_tmpa_seq {2} &}
}
\tl_put_right:Nn \l_tmpa_tl { \tabularnewline\hline}
\begin{tabular}{|*{\int_eval:n{\seq_count:N \l_gcd_pair_seq+1}}{c|}}
\tl_use:N \l_tmpa_tl
\end{tabular}
\group_end:
}
\ExplSyntaxOff
\begin{document}
\gcdcalc{16}{6}
\gcdcalc{24}{8}
\gcdcalc{1024}{120}
\end{document}
答案4
\newcount
仅使用 TeX 基元(和宏)的一个解决方案:
\newcount\tmpnum
\def\gcd#1#2{\def\gcdR{{#1}{}{#2}}\gcdE{#1}{#2}\hbox{\expandafter\gcdP\gcdR}}
\def\gcdE#1#2{%
\tmpnum=#1 \divide\tmpnum by#2 \gcdN{\the\tmpnum}%
\multiply \tmpnum by-#2 \advance\tmpnum by#1 \gcdN{\the\tmpnum}%
\ifnum\tmpnum=0 \let\next=\relax
\else \edef\next{\noexpand\gcdE{#2}{\the\tmpnum}}\fi
\next
}
\def\gcdN#1{\edef\gcdR{\gcdR{#1}}}
\def\gcdP#1#2#3#4#5{\gcdB{#2}{#1}{#5}%
\ifnum#5=0 \expandafter \gcdS \else \expandafter \gcdP \fi {#3}{#4}{#5}%
}
\def\gcdS#1#2#3{\gcdB{#2}{#1}{}\vrule}
\def\gcdB#1#2#3{\vrule\vbox{\hrule\gcdC{#1}\gcdC{#2}\gcdC{#3}}}
\def\gcdC#1{\hbox to5em{\hss\lower4pt\vbox to17pt{}#1\ }\hrule}
% test:
\gcd{16}{6}
\vskip 5mm
\gcd{137205626}{12539182}
\end
该\gcd
宏分为两个步骤:首先,在 中准备结果\gcdR
。其次,使用 打印结果\gcdP
。