# Permutations of squares

Consider the following problem: Imagine you choose 3 squares out of a 3x3 field. So you can have a line upwords or downwords, two squares downwords and one to the right etc. So let's number every square (see MWE). Now it is just a mathematicaly problem: $binom(9,3)$.

But since the squares all look the same, there is no difference between (1,2,3) and (3,2,1) and so on. So when we take a look here it is a pretty good solution, but we got the problem with the duplicates.

I thought of writing a python script which exoprts the correct permuations into a .csv file and then TikZ or pgfplots(maybe it can be done there, too) can read the file and solve the "square problem".

So my question is: How can I "cross out" the duplicates in order to get sth. like in the MWE?

\documentclass[border=5pt,tikz]{standalone}
\newcommand{\setcircle}[3]{
\pgfmathsetmacro\testnum{int(mod(#1,3))}
\ifnum\testnum=0
\pgfmathsetmacro\oxpos{3}
\pgfmathsetmacro\oypos{floor(#1/3)-1}
\else
\pgfmathsetmacro\oxpos{mod(#1,3)}
\pgfmathsetmacro\oypos{floor(#1/3)}
\fi

\pgfmathsetmacro\testnum{int(mod(#2,3))}
\ifnum\testnum=0
\pgfmathsetmacro\txpos{3}
\pgfmathsetmacro\typos{floor(#2/3)-1}
\else
\pgfmathsetmacro\txpos{mod(#2,3)}
\pgfmathsetmacro\typos{floor(#2/3)}
\fi

\pgfmathsetmacro\testnum{int(mod(#3,3))}
\ifnum\testnum=0
\pgfmathsetmacro\thxpos{3}
\pgfmathsetmacro\thypos{floor(#3/3)-1}
\else
\pgfmathsetmacro\thxpos{mod(#3,3)}
\pgfmathsetmacro\thypos{floor(#3/3)}
\fi

\draw (\oxpos,-\oypos) circle(.5) node {#1};
\draw (\txpos,-\typos) circle(.5) node {#2};
\draw (\thxpos,-\thypos) circle(.5) node {#3};
}
\begin{document}
\begin{tikzpicture}
\foreach \x in {1,...,9}
{
\pgfmathsetmacro\testnum{int(mod(\x,3))}
\ifnum\testnum=0
\pgfmathsetmacro\xpos{3}
\pgfmathsetmacro\ypos{floor(\x/3)-1}
\else
\pgfmathsetmacro\xpos{mod(\x,3)}
\pgfmathsetmacro\ypos{floor(\x/3)}
\fi

\draw (\xpos,-\ypos) circle(.5) node {\x};
}

\begin{scope}[xshift=-3cm,yshift=-4cm]
\setcircle{1}{2}{5}
\end{scope}

\begin{scope}[yshift=-4cm]
\setcircle{1}{2}{3}
\end{scope}

\begin{scope}[xshift=3cm,yshift=-4cm]
\setcircle{3}{6}{8}
\end{scope}
\end{tikzpicture}
\end{document}


Output:

• I most likely completely miss the point. But if you were to sort the lists, then you wouldn't have any duplicates because then (1,3,2) and (2,3,1) will all be (1,2,3). What am I doing wrong? And to get all inequivalent sorted lists you could just use texwelt.de/wissen/antwort_link/24383, for instance. – user121799 Mar 12 at 17:13
• @marmot When I "plot" the results (1,3,2), (2,3,1) and (1,2,3), the output will look the same. So when I plot all results, I would get these duplicates. I want to display all combinations without the repeating sequences in the form of a grid (so (1,2,3) got for example the position (1,1) etc.) It's just a visualisation of all permutations … I hope it's clear now … – current_user Mar 12 at 20:44
• Yes, but if you would plot the permutation only if the arguments are ascending, you would not plot duplicates. In this case, out of (1,3,2), (2,3,1) and (1,2,3) you would only plot (1,2,3) but suppress the duplicates (1,3,2) and (2,3,1). – user121799 Mar 12 at 20:49
• @marmot Exactly what I mean! – current_user Mar 12 at 20:50

This draws all the inequivalent combinations.

\documentclass[tikz,border=3.14mm]{standalone}
\newcounter{mystep}
\begin{document}
\begin{tikzpicture}[insert circle/.style={insert path={%
({mod(#1-1,3)*0.75},{int((#1-1)/3)*0.75}) node[circle,draw]{#1}}}]
\foreach \X [evaluate=\X as \Ymin using {int(\X+1)}] in {1,...,9}
{\foreach \Y [evaluate=\Y as \Zmin using {int(\X+1)}]in {\Ymin,...,9}
{\foreach \Z in {\Zmin,...,9}
{\ifnum\X<\Y
\ifnum\Y<\Z
\stepcounter{mystep}
\begin{scope}[xshift={mod(\number\value{mystep}-1,7)*3cm},
yshift={-int((\number\value{mystep}-1)/7)*3cm}]
\path[insert circle/.list={\X,\Y,\Z}];
\end{scope}
\fi
\fi
}}}
\typeout{\number\value{mystep}\space combinations}
\end{tikzpicture}
\end{document}


• Nice one, marmot! – voices Jul 10 at 23:43