有没有办法生成整数 (m,n) 的每个互质对的图?如果两个数的最大公约数等于一 (gcd(m,n)=1),则这两个数互质,在这种情况下,它们满足 Bezout 恒等式 mx+ny=1,其中有两个整数 x 和 y。我想绘制整数格的一部分并阐明互质对。我没有乳胶技能,所以我不知道是否有简单的方法,但我认为我需要一些“if then else”的胡言乱语并定义 gcd 函数。需要两个 \foreach 循环才能绘制格,并且必须对每个生成的对进行检查 gcd(m,n)==1。任何建议都将不胜感激。
答案1
更新3我们确实尝试画一些TikZ
图画......(见下面)。
更新2而不是(1,0) (0,1)
更聪明的做法是先从 开始(1,0) (1,1)
,这将只生成带有(n,m)
的对m<n
(除了(1,1)
)。我们从相同数量的生成步骤中获得更多对。我没有更新图像,只更新了按字典顺序排序的列表。由于我太忙于这个网站上的 LaTeX 宏,我没有考虑过关于如何确保在给定 的情况下已经找到n
所有带有 的对的数学问题m<n
。
更新1仅对从中复制的排序代码如何对 LaTeX 中的坐标列表进行字典排序?那里有与空间相关的编辑。复制到这里。
本答案专注于尽可能快地生成所有 n 较小的互质对 (n, m),比如说几十个。增加\count@
以下值可获得更多。
生成对之后,代码会将它们打印在页面上:你们 TikZ 人会比我更了解如何进行一些漂亮的绘图。
然后对是按字典顺序排序,并打印到文件中。您按 n 和 m 的顺序列出它们。我也在这里插入结果。
\documentclass{article}
\usepackage[T1]{fontenc}
\usepackage[margin=1cm]{geometry}
% generate all coprime pairs coordinates
\def\gencoprimes #1(#2,#3)#4(#5,#6){%
\if!#5\expandafter\genend\fi
(#2,#3) (\the\numexpr#2+#5,\the\numexpr#3+#6) \gencoprimes (#5,#6)}%
\def\genend (#1)#2\gencoprimes (!,!){(#1)}
\makeatletter
\count@ 10 % number of iterations
% this is the choice for the image below
\def\coprimelist {(1,0) (0,1)}
% this however is what we used for later sorting lexicographically:
\def\coprimelist {(1,0) (1,1)}
\loop
\edef\coprimelist {\expandafter\gencoprimes\coprimelist (!,!)}
\advance\count@ \m@ne
\ifnum\count@ > \z@
\repeat
\makeatother
% next is NOT needed for generating the coprime pairs:
% it only does *sorting*
% sorting code picked from https://tex.stackexchange.com/a/283273/4686
\makeatletter
% Here we define the comparison macro for pairs (a,b)
% We assume decimal numbers acceptable to \ifdim tests
\long\def\xintdothis #1#2\xintorthat #3{\fi #1}%
\let\xintorthat \@firstofone
\long\def\@thirdoffour #1#2#3#4{#3}%
\long\def\@fourthoffour #1#2#3#4{#4}%
\def\IfFirstPairIsGreaterTF #1#2{\@IfFirstPairIsGreaterTF #1,#2,}%
\def\@IfFirstPairIsGreaterTF #1,#2,#3,#4,{%
\ifdim #1\p@=#3\p@
\xintdothis{%
\ifdim #2\p@>#4\p@\expandafter\@firstoftwo
\else\expandafter\@secondoftwo\fi}\fi
\ifdim #1\p@>#3\p@\expandafter\@thirdoffour
\else\expandafter\@fourthoffour\fi
\xintorthat{}%
}%
% not needed for numerical inputs
% \catcode`! 3
% \catcode`? 3
\def\QSpairs {\romannumeral0\romannumeral0\qspairs }%
% first we check if empty list
\def\qspairs #1{\expandafter\qspairs@a\romannumeral-`0#1(!)(?)}%
\def\qspairs@a #1(#2{\ifx!#2\expandafter\qspairs@abort\else
\expandafter\qspairs@b\fi (#2}%
\edef\qspairs@abort #1(?){\space\space}%
%
% we check if empty of single and if not pick up the first as Pivot:
\def\qspairs@b #1(#2)#3(#4){\ifx?#4\xintdothis\qspairs@empty\fi
\ifx!#4\xintdothis\qspairs@single\fi
\xintorthat \qspairs@separate {}{}{#2}(#4)}%
\def\qspairs@empty #1(?){ }%
\edef\qspairs@single #1#2#3#4(?){\space\space(#3)}%
\def\qspairs@separate #1#2#3#4(#5)%
{%
\ifx!#5\expandafter\qspairs@separate@done\fi
\IfFirstPairIsGreaterTF {#5}{#3}%
\qspairs@separate@appendtogreater
\qspairs@separate@appendtosmaller {#5}{#1}{#2}{#3}%
}%
%
\def\qspairs@separate@appendtogreater #1#2{\qspairs@separate {#2 (#1)}}%
\def\qspairs@separate@appendtosmaller #1#2#3{\qspairs@separate {#2}{#3 (#1)}}%
%
\def\qspairs@separate@done\IfFirstPairIsGreaterTF #1#2%
\qspairs@separate@appendtogreater
\qspairs@separate@appendtosmaller #3#4#5#6(?)%
{%
\expandafter\qspairs@f\expandafter
{\romannumeral0\qspairs@b #4(!)(?)}{\qspairs@b #5(!)(?)}{ (#2)}%
}%
%
\def\qspairs@f #1#2#3{#2#3#1}%
%
% \catcode`! 12
% \catcode`? 12
\makeatother
\begin{document}\thispagestyle{empty}
% print the coordinates list on the page
\meaning\coprimelist
\newwrite\coprimes
\immediate\openout\coprimes=\jobname.coprimes
% write out to a file the coordinates pairs, in lexicographic order
\immediate\write\coprimes{\QSpairs{\coprimelist}}
\end{document}
(1,0) (1,1) (2,1) (3,1) (3,2) (4,1) (4,3) (5,1) (5,2) (5,3) (5,4) (6,1) (6,5) (7,1) (7,2) (7,3) (7,4) (7,5) (7,6) (8,1) (8,3) (8,5) (8,7) (9,1) (9,2) (9,4) (9,5) (9,7) (9,8) (10,1) (10,3) (10,7) (10,9) (11,1) (11,2) (11,3) (11,4) (11,5) (11,6) (11,7) (11,8) (11,9) (11,10) (12,5) (12,7) (13,2) (13,3) (13,4) (13,5) (13,6) (13,7) (13,8) (13,9) (13,10) (13,11) (14,3) (14,5) (14,9) (14,11) (15,2) (15,4) (15,7) (15,8) (15,11) (15,13) (16,3) (16,5) (16,7) (16,9) (16,11) (16,13) (17,2) (17,3) (17,4) (17,5) (17,6) (17,7) (17,8) (17,9) (17,10) (17,11) (17,12) (17,13) (17,14) (17,15) (18,5) (18,7) (18,11) (18,13) (19,2) (19,3) (19,4) (19,5) (19,6) (19,7) (19,8) (19,9) (19,10) (19,11) (19,12) (19,13) (19,14) (19,15) (19,16) (19,17) (20,3) (20,7) (20,9) (20,11) (20,13) (20,17) (21,4) (21,5) (21,8) (21,13) (21,16) (21,17) (22,3) (22,5) (22,7) (22,9) (22,13) (22,15) (22,17) (22,19) (23,3) (23,4) (23,5) (23,6) (23,7) (23,8) (23,9) (23,10) (23,13) (23,14) (23,15) (23,16) (23,17) (23,18) (23,19) (23,20) (24,5) (24,7) (24,11) (24,13) (24,17) (24,19) (25,3) (25,4) (25,6) (25,7) (25,8) (25,9) (25,11) (25,14) (25,16) (25,17) (25,18) (25,19) (25,21) (25,22) (26,3) (26,5) (26,7) (26,9) (26,11) (26,15) (26,17) (26,19) (26,21) (26,23) (27,4) (27,5) (27,7) (27,8) (27,10) (27,11) (27,16) (27,17) (27,19) (27,20) (27,22) (27,23) (28,5) (28,11) (28,13) (28,15) (28,17) (28,23) (29,4) (29,5) (29,6) (29,7) (29,8) (29,9) (29,11) (29,12) (29,13) (29,16) (29,17) (29,18) (29,20) (29,21) (29,22) (29,23) (29,24) (29,25) (30,7) (30,11) (30,13) (30,17) (30,19) (30,23) (31,4) (31,5) (31,6) (31,7) (31,8) (31,9) (31,11) (31,12) (31,13) (31,14) (31,17) (31,18) (31,19) (31,20) (31,22) (31,23) (31,24) (31,25) (31,26) (31,27) (32,5) (32,7) (32,9) (32,13) (32,15) (32,17) (32,19) (32,23) (32,25) (32,27) (33,5) (33,7) (33,10) (33,13) (33,14) (33,19) (33,20) (33,23) (33,26) (33,28) (34,5) (34,7) (34,9) (34,13) (34,15) (34,19) (34,21) (34,25) (34,27) (34,29) (35,6) (35,8) (35,11) (35,13) (35,16) (35,19) (35,22) (35,24) (35,27) (35,29) (36,11) (36,13) (36,23) (36,25) (37,5) (37,7) (37,8) (37,10) (37,11) (37,13) (37,14) (37,15) (37,16) (37,17) (37,20) (37,21) (37,22) (37,23) (37,24) (37,26) (37,27) (37,29) (37,30) (37,32) (38,5) (38,7) (38,9) (38,11) (38,15) (38,17) (38,21) (38,23) (38,27) (38,29) (38,31) (38,33) (39,7) (39,11) (39,14) (39,16) (39,17) (39,22) (39,23) (39,25) (39,28) (39,32) (40,7) (40,9) (40,11) (40,17) (40,23) (40,29) (40,31) (40,33) (41,9) (41,11) (41,12) (41,13) (41,15) (41,16) (41,17) (41,18) (41,19) (41,22) (41,23) (41,24) (41,25) (41,26) (41,28) (41,29) (41,30) (41,32) (42,11) (42,13) (42,19) (42,23) (42,29) (42,31) (43,8) (43,9) (43,10) (43,12) (43,13) (43,15) (43,16) (43,18) (43,19) (43,20) (43,23) (43,24) (43,25) (43,27) (43,28) (43,30) (43,31) (43,33) (43,34) (43,35) (44,7) (44,13) (44,17) (44,19) (44,25) (44,27) (44,31) (44,37) (45,7) (45,8) (45,13) (45,14) (45,16) (45,17) (45,19) (45,26) (45,28) (45,29) (45,31) (45,32) (45,37) (45,38) (46,7) (46,11) (46,13) (46,17) (46,19) (46,21) (46,25) (46,27) (46,29) (46,33) (46,35) (46,39) (47,7) (47,9) (47,10) (47,11) (47,13) (47,14) (47,17) (47,18) (47,20) (47,21) (47,26) (47,27) (47,29) (47,30) (47,33) (47,34) (47,36) (47,37) (47,38) (47,40) (48,11) (48,13) (48,17) (48,31) (48,35) (48,37) (49,9) (49,11) (49,13) (49,15) (49,18) (49,19) (49,20) (49,22) (49,27) (49,29) (49,30) (49,31) (49,34) (49,36) (49,38) (49,40) (50,9) (50,11) (50,13) (50,19) (50,21) (50,23) (50,27) (50,29) (50,31) (50,37) (50,39) (50,41) (51,8) (51,11) (51,14) (51,16) (51,19) (51,20) (51,23) (51,28) (51,31) (51,32) (51,35) (51,37) (51,40) (51,43) (52,9) (52,11) (52,19) (52,23) (52,29) (52,33) (52,41) (52,43) (53,8) (53,10) (53,11) (53,12) (53,14) (53,16) (53,19) (53,20) (53,22) (53,23) (53,24) (53,29) (53,30) (53,31) (53,33) (53,34) (53,37) (53,39) (53,41) (53,42) (53,43) (53,45) (54,17) (54,19) (54,35) (54,37) (55,12) (55,13) (55,16) (55,17) (55,21) (55,23) (55,24) (55,31) (55,32) (55,34) (55,38) (55,39) (55,42) (55,43) (56,13) (56,15) (56,17) (56,23) (56,33) (56,39) (56,41) (56,43) (57,10) (57,13) (57,16) (57,17) (57,20) (57,22) (57,25) (57,32) (57,35) (57,37) (57,40) (57,41) (57,44) (57,47) (58,11) (58,17) (58,21) (58,37) (58,41) (58,47) (59,11) (59,14) (59,16) (59,18) (59,21) (59,23) (59,24) (59,25) (59,26) (59,27) (59,32) (59,33) (59,34) (59,35) (59,36) (59,38) (59,41) (59,43) (59,45) (59,48) (60,13) (60,23) (60,37) (60,47) (61,13) (61,14) (61,16) (61,17) (61,18) (61,19) (61,22) (61,24) (61,25) (61,28) (61,33) (61,36) (61,37) (61,39) (61,42) (61,43) (61,44) (61,45) (61,47) (61,48) (62,11) (62,13) (62,17) (62,19) (62,23) (62,27) (62,35) (62,39) (62,43) (62,45) (62,49) (62,51) (63,11) (63,17) (63,23) (63,26) (63,37) (63,40) (63,46) (63,52) (64,15) (64,17) (64,19) (64,23) (64,25) (64,27) (64,37) (64,39) (64,41) (64,45) (64,47) (64,49) (65,12) (65,14) (65,17) (65,18) (65,19) (65,23) (65,24) (65,27) (65,38) (65,41) (65,42) (65,46) (65,47) (65,48) (65,51) (65,53) (66,25) (66,29) (66,37) (66,41) (67,12) (67,14) (67,18) (67,24) (67,26) (67,28) (67,29) (67,30) (67,37) (67,38) (67,39) (67,41) (67,43) (67,49) (67,53) (67,55) (68,19) (68,25) (68,43) (68,49) (69,19) (69,20) (69,29) (69,31) (69,38) (69,40) (69,49) (69,50) (70,13) (70,27) (70,29) (70,41) (70,43) (70,57) (71,15) (71,16) (71,19) (71,20) (71,21) (71,22) (71,26) (71,27) (71,29) (71,30) (71,31) (71,32) (71,39) (71,40) (71,41) (71,42) (71,44) (71,45) (71,49) (71,50) (71,51) (71,52) (71,55) (71,56) (72,19) (72,53) (73,13) (73,16) (73,17) (73,27) (73,28) (73,30) (73,31) (73,32) (73,33) (73,40) (73,41) (73,42) (73,43) (73,45) (73,46) (73,56) (73,57) (73,60) (74,23) (74,29) (74,31) (74,43) (74,45) (74,51) (75,17) (75,22) (75,29) (75,31) (75,44) (75,46) (75,53) (75,58) (76,21) (76,23) (76,27) (76,29) (76,31) (76,33) (76,43) (76,45) (76,47) (76,49) (76,53) (76,55) (77,18) (77,30) (77,34) (77,43) (77,47) (77,59) (78,17) (78,23) (78,29) (78,35) (78,43) (78,49) (78,55) (78,61) (79,18) (79,22) (79,23) (79,24) (79,28) (79,29) (79,30) (79,31) (79,48) (79,49) (79,50) (79,51) (79,55) (79,56) (79,57) (79,61) (80,17) (80,31) (80,33) (80,47) (80,49) (80,63) (81,31) (81,34) (81,47) (81,50) (82,23) (82,25) (82,31) (82,37) (82,45) (82,51) (82,57) (82,59) (83,18) (83,19) (83,22) (83,23) (83,30) (83,34) (83,35) (83,36) (83,47) (83,48) (83,49) (83,53) (83,60) (83,61) (83,64) (83,65) (84,19) (84,25) (84,31) (84,37) (84,47) (84,53) (84,59) (84,65) (85,18) (85,23) (85,26) (85,33) (85,36) (85,37) (85,48) (85,49) (85,52) (85,59) (85,62) (85,67) (86,25) (86,31) (86,55) (86,61) (87,19) (87,23) (87,32) (87,34) (87,53) (87,55) (87,64) (87,68) (88,19) (88,37) (88,51) (88,69) (89,24) (89,25) (89,26) (89,27) (89,32) (89,33) (89,34) (89,55) (89,56) (89,57) (89,62) (89,63) (89,64) (89,65) (91,25) (91,27) (91,40) (91,51) (91,64) (91,66) (92,21) (92,33) (92,35) (92,39) (92,53) (92,57) (92,59) (92,71) (93,25) (93,26) (93,34) (93,41) (93,52) (93,59) (93,67) (93,68) (94,39) (94,41) (94,53) (94,55) (95,29) (95,36) (95,39) (95,56) (95,59) (95,66) (97,21) (97,26) (97,35) (97,36) (97,37) (97,41) (97,56) (97,60) (97,61) (97,62) (97,71) (97,76) (98,27) (98,29) (98,41) (98,43) (98,55) (98,57) (98,69) (98,71) (99,29) (99,41) (99,58) (99,70) (100,27) (100,37) (100,39) (100,41) (100,59) (100,61) (100,63) (100,73) (101,30) (101,37) (101,39) (101,44) (101,57) (101,62) (101,64) (101,71) (103,37) (103,39) (103,64) (103,66) (104,29) (104,43) (104,61) (104,75) (105,29) (105,31) (105,41) (105,44) (105,61) (105,64) (105,74) (105,76) (106,31) (106,41) (106,65) (106,75) (107,41) (107,47) (107,60) (107,66) (108,29) (108,41) (108,67) (108,79) (109,30) (109,40) (109,45) (109,46) (109,63) (109,64) (109,69) (109,79) (111,31) (111,41) (111,43) (111,46) (111,65) (111,68) (111,70) (111,80) (112,31) (112,41) (112,47) (112,65) (112,71) (112,81) (115,34) (115,44) (115,71) (115,81) (116,45) (116,49) (116,67) (116,71) (117,43) (117,49) (117,68) (117,74) (119,44) (119,46) (119,50) (119,69) (119,73) (119,75) (121,46) (121,50) (121,71) (121,75) (123,34) (123,47) (123,76) (123,89) (128,47) (128,49) (128,79) (128,81) (129,49) (129,50) (129,79) (129,80) (131,50) (131,55) (131,76) (131,81) (144,55) (144,89)59) (93,67) (93,68) (94,39) (94,41) (94,53) (94,55) (95,29) (95,36) (95,39) (95,56) (95,59) (95,66) (97,21) (97,26) (97,35) (97,36) (97,37) (97,41) (97,56) (97,60) (97,61) (97,62) (97,71) (97,76) (98,27) (98,29) (98,41) (98,43) (98,55) (98,57) (98,69) (98,71) (99,29) (99,41) (99,58) (99,70) (100,27) (100,37) (100,39) (100,41) (100,59) (100,61) (100,63) (100,73) (101,30) (101,37) (101,39) (101,44) (101,57) (101,62) (101,64) (101,71) (103,37) (103,39) (103,64) (103,66) (104,29) (104,43) (104,61) (104,75) (105,29) (105,31) (105,41) (105,44) (105,61) (105,64) (105,74) (105,76) (106,31) (106,41) (106,65) (106,75) (107,41) (107,47) (107,60) (107,66) (108,29) (108,41) (108,67) (108,79) (109,30) (109,40) (109,45) (109,46) (109,63) (109,64) (109,69) (109,79) (111,31) (111,41) (111,43) (111,46) (111,65) (111,68) (111,70) (111,80) (112,31) (112,41) (112,47) (112,65) (112,71) (112,81) (115,34) (115,44) (115,71) (115,81) (116,45) (116,49) (116,67) (116,71) (117,43) (117,49) (117,68) (117,74) (119,44) (119,46) (119,50) (119,69) (119,73) (119,75) (121,46) (121,50) (121,71) (121,75) (123,34) (123,47) (123,76) (123,89) (128,47) (128,49) (128,79) (128,81) (129,49) (129,50) (129,79) (129,80) (131,50) (131,55) (131,76) (131,81) (144,55) (144,89)59) (93,67) (93,68) (94,39) (94,41) (94,53) (94,55) (95,29) (95,36) (95,39) (95,56) (95,59) (95,66) (97,21) (97,26) (97,35) (97,36) (97,37) (97,41) (97,56) (97,60) (97,61) (97,62) (97,71) (97,76) (98,27) (98,29) (98,41) (98,43) (98,55) (98,57) (98,69) (98,71) (99,29) (99,41) (99,58) (99,70) (100,27) (100,37) (100,39) (100,41) (100,59) (100,61) (100,63) (100,73) (101,30) (101,37) (101,39) (101,44) (101,57) (101,62) (101,64) (101,71) (103,37) (103,39) (103,64) (103,66) (104,29) (104,43) (104,61) (104,75) (105,29) (105,31) (105,41) (105,44) (105,61) (105,64) (105,74) (105,76) (106,31) (106,41) (106,65) (106,75) (107,41) (107,47) (107,60) (107,66) (108,29) (108,41) (108,67) (108,79) (109,30) (109,40) (109,45) (109,46) (109,63) (109,64) (109,69) (109,79) (111,31) (111,41) (111,43) (111,46) (111,65) (111,68) (111,70) (111,80) (112,31) (112,41) (112,47) (112,65) (112,71) (112,81) (115,34) (115,44) (115,71) (115,81) (116,45) (116,49) (116,67) (116,71) (117,43) (117,49) (117,68) (117,74) (119,44) (119,46) (119,50) (119,69) (119,73) (119,75) (121,46) (121,50) (121,71) (121,75) (123,34) (123,47) (123,76) (123,89) (128,47) (128,49) (128,79) (128,81) (129,49) (129,50) (129,79) (129,80) (131,50) (131,55) (131,76) (131,81) (144,55) (144,89)
我不知道用 来操作此类坐标列表的最佳方法是什么TikZ
。这里使用plot coordinates
:
\documentclass[tikz, ignorerest=false]{standalone}
% generate all coprime pairs coordinates
\def\gencoprimes #1(#2,#3)#4(#5,#6){%
\if!#5\expandafter\genend\fi
(#2,#3) (\the\numexpr#2+#5,\the\numexpr#3+#6) \gencoprimes (#5,#6)}%
\def\genend (#1)#2\gencoprimes (!,!){(#1)}
\makeatletter
\count@ 8 % number of iterations
\def\coprimelist {(1,0) (0,1)}
%\def\coprimelist {(1,0) (1,1)}
\loop
\edef\coprimelist {\expandafter\gencoprimes\coprimelist (!,!)}
\advance\count@ \m@ne
\ifnum\count@ > \z@
\repeat
\makeatother
\begin{document}
\begin{tikzpicture}[x=2mm,y=2mm]
\draw [color=blue] plot [only marks, mark=x] coordinates {\coprimelist};
\end{tikzpicture}
\makeatletter
\count@ 3 % number of extra iterations
\loop
\edef\coprimelist {\expandafter\gencoprimes\coprimelist (!,!)}
\advance\count@ \m@ne
\ifnum\count@ > \z@
\repeat
\makeatother
\begin{tikzpicture}[x=.5mm,y=.5mm]
\draw [color=blue] plot [only marks, mark=x] coordinates {\coprimelist};
\end{tikzpicture}
\end{document}
这有助于理解,这种方法似乎并不是找到边长为 的矩形中的所有互质对的最有效方法N
。
答案2
#1
它输出区间内的互质数2..#2
。将它扩展为输出对并不困难。
\documentclass{article}
\usepackage{luacode}
\begin{luacode}
function g(a,b) return b~=0 and g(b,a%b) or tonumber(a) end
function getCoP(c,d)
for i=2,d do
if g(i,c)<2 then tex.print(tostring(i)..", ") end
end
end
\end{luacode}
\def\getCoPrimes#1#2{\directlua{getCoP(#1,#2)}}
\begin{document}
\getCoPrimes{9699690}{200}
\end{document}
答案3
我添加了另一个答案,因为尽管生成不可约分数的方法很有趣,但它并不适合轻松获取给定矩形范围内的所有互质对(据我所知)。a/b, c/d -> (a+b)/(c+d)
a/b, c/d -> (a+c)/(b+d)
因此,这里是另一种方法,它迭代地构造一个数组,\co.n.m
其中的宏可以保存1
是否互质,0
如果不是,但如果算法仅在(n,m)
互质时定义宏(以减少内存压力),会发生什么情况。
然后构造一个分支宏\IfCoPrimeTF{A}{B}{True}{False}
。最后生成一个 TikZ 图片。
我已经比较过了Paul Gaborit 的回答速度增益并不大,只是少了一点1/3
。这似乎表明,在该答案中,绘图部分大约占三分之二,而 gcd 计算部分大约占三分之一的计算时间:因为决策时间\IfCoPrime{\n}{\m}
应该可以忽略不计。但我并不总是很擅长理解各种事物的时间效率TeX
......
\documentclass[tikz, border=10pt]{standalone}
% generate iteratively "marker macros" for coprime pairs (n,m)
\makeatletter
\@namedef{co.1.0}{1}
\@namedef{co.0.1}{1}
\@namedef{co.1.1}{1}
\foreach\n in {2,...,180}{%
\foreach\m in {1,...,\n}{%
\ifcsname co.\the\numexpr\n-\m.\m\endcsname
\global\@namedef{co.\n.\m}{1}%
\global\@namedef{co.\m.\n}{1}%
\fi
}}
% Define a \IfCoPrimeTF {N}{M}{ARE COPRIMES}{ARE NOT COPRIMES}
\def\IfCoPrimeTF #1#2{\ifcsname co.#1.#2\endcsname
\expandafter\@firstoftwo\else\expandafter\@secondoftwo\fi }
\makeatother
\begin{document}
\begin{tikzpicture}[x=.5cm,y=.5cm]
\foreach \n in {0,...,20}{
\foreach \m in {0,...,\n}{
\IfCoPrimeTF{\n}{\m}
{% truebranch
% \fill[blue] (\n,\m) rectangle ++(1,1)
% (\m,\n) rectangle ++(1,1);
\fill [blue] (\n, \m) circle [radius=4pt];
\fill [blue] (\m, \n) circle [radius=4pt];
}
{% false branch
\fill [red] (\n, \m) circle [radius=2pt];
\fill [red] (\m, \n) circle [radius=2pt];
}
}
}
\end{tikzpicture}
\end{document}
为了好玩,我还编写了“gcd 数组”的代码。当然,这很愚蠢,因为正如 Paul 的回答所示,TikZ 知道gcd
。因此,为了节省一点时间,我们填充了 TeX 的内存...
\documentclass[tikz, border=10pt]{standalone}
% generate a gcd array
\makeatletter
\@namedef{gcd.1.0}{1}
\@namedef{gcd.0.1}{1}
\@namedef{gcd.1.1}{1}
\foreach\m in {2,...,50}{%
\expandafter\xdef\csname gcd.0.\m\endcsname {\m}%
\global\expandafter\let\csname gcd.\m.0\expandafter\endcsname
\csname gcd.0.\m\endcsname
}%
\foreach\n in {1,...,50}{%
\foreach\m in {1,...,\n}{%
\expandafter\xdef\csname gcd.\n.\m\endcsname
{\csname gcd.\the\numexpr\n-\m.\m\endcsname}%
\global\expandafter\let\csname gcd.\m.\n\expandafter\endcsname
\csname gcd.\n.\m\endcsname
}}
\def\MyGCD #1#2{\@nameuse{gcd.#1.#2}}
\makeatother
\begin{document}
\begin{tikzpicture}[x=.5cm,y=.5cm]
\foreach \n in {0,...,20}{
\foreach \m in {0,...,20}{
\draw (\n,\m) +(-.5,-.5) rectangle ++(.5,.5);
\draw (\n,\m) node{\MyGCD{\n}{\m}};
}
}
\end{tikzpicture}
\end{document}