显示素数差

显示素数差

我正在尝试在我的项目中创建多个节点,如下所示:

 \Tree [.S This [.VP ] ]

我搜索了,没有结果。我想要类似这

答案1

这是实现您想要的功能的一种方法。我改编了此答案中的代码:生成素数列表按照你画的方式画出每对素数之间的差异。

该命令将绘制素数之间的差异,默认情况下每行放置 13 个素数。这可以更改,每行占用的垂直空间也可以更改。

如果第一个素数不是 2,那么你需要提供其前一个素数的值来绘制第一个节点。这个素数由宏的可选参数给出。

\documentclass[]{article}
\usepackage[margin=1in]{geometry}
\usepackage{tikz}
\usetikzlibrary{calc}

\newcounter{primecount}
\newlength{\yoffset}
\newcounter{rownum}
% command to draw the primes between two numbers and their differences
% the optional argument specifies the number of the first previous prime
% the default is 2, in which case nothing is shown; otherwise a node with
% that prime is drawn
\newcommand{\primes}[3][2]{%
    \setlength{\yoffset}{.75in} % vertical offset per row
    \def\displaynum{13} % number of primes per row
    \setcounter{rownum}{0}
    \setcounter{primecount}{1}
    \def\prevprime{#1}
    \ifnum\prevprime=2
        \node at (-1,0) (\prevprime) {};
    \else
        \node (\prevprime) {\prevprime};
    \fi
    \foreach\numb in {#2,...,#3}{%
        \pgfmathisprime{\numb}%
        \ifnum\pgfmathresult=1
            \pgfmathparse{int(mod(\value{primecount},\displaynum))}
            \ifnum\pgfmathresult=0
                \stepcounter{rownum}
                \node at (0,-\value{rownum}*\yoffset) (\prevprime) {\prevprime};
                \setcounter{primecount}{1}
            \fi
            \node[right of=\prevprime,inner sep=2pt] (\numb) {\numb};
            \pgfmathparse{int(\numb-\prevprime)}
            \def\diff{\pgfmathresult}
            \ifnum\diff=0
                \setcounter{primecount}{0}
            \else
                \coordinate (C) at ($(\prevprime)!0.5!(\numb)$);
                \node[ below of= C,inner sep=2pt ] (\diff) {\diff};
                \draw (\prevprime) -- (\diff) -- (\numb);
            \fi
            \global\edef\prevprime{\numb}
            \stepcounter{primecount}
        \fi%     
    }%
}

\begin{document}
\begin{tikzpicture}
\primes{1}{200}
\end{tikzpicture}

\end{document}

代码输出

相关内容