[Tex/LaTex] How to write a code to find the Pythagorean Triples

codetables

I want to write a code to make a table contains Pythagorean Triples (see picture) automatically. That is mean from the formulas $a^2 + b^2 = c^2$ , we can get the Pythagorean Triples enter image description here

I used Mathematica, convert to TeX, I get
enter image description here

the code of Mathematica is

\left(
\begin{array}{ccc}
 3 & 4 & 5 \\
 5 & 12 & 13 \\
 7 & 24 & 25 \\
 8 & 15 & 17 \\
 9 & 12 & 15 \\
 9 & 40 & 41 \\
 11 & 60 & 61 \\
 12 & 35 & 37 \\
 13 & 84 & 85 \\
 15 & 20 & 25 \\
 15 & 36 & 39 \\
 15 & 112 & 113 \\
 16 & 63 & 65 \\
 17 & 144 & 145 \\
 19 & 180 & 181 \\
 20 & 21 & 29 \\
 20 & 99 & 101 \\
 21 & 28 & 35 \\
 21 & 72 & 75 \\
 21 & 220 & 221 \\
 23 & 264 & 265 \\
 24 & 45 & 51 \\
 24 & 143 & 145 \\
 25 & 60 & 65 \\
 27 & 36 & 45 \\
 27 & 120 & 123 \\
 28 & 45 & 53 \\
 28 & 195 & 197 \\
 32 & 255 & 257 \\
 33 & 44 & 55 \\
 33 & 56 & 65 \\
 33 & 180 & 183 \\
 35 & 84 & 91 \\
 35 & 120 & 125 \\
 36 & 77 & 85 \\
 36 & 105 & 111 \\
 39 & 52 & 65 \\
 39 & 80 & 89 \\
 39 & 252 & 255 \\
 40 & 75 & 85 \\
 44 & 117 & 125 \\
 45 & 60 & 75 \\
 45 & 108 & 117 \\
 45 & 200 & 205 \\
 48 & 55 & 73 \\
 48 & 189 & 195 \\
 49 & 168 & 175 \\
 51 & 68 & 85 \\
 51 & 140 & 149 \\
 52 & 165 & 173 \\
 55 & 132 & 143 \\
 55 & 300 & 305 \\
 56 & 105 & 119 \\
 57 & 76 & 95 \\
 57 & 176 & 185 \\
 60 & 63 & 87 \\
 60 & 91 & 109 \\
 60 & 175 & 185 \\
 60 & 221 & 229 \\
 60 & 297 & 303 \\
 63 & 84 & 105 \\
 63 & 216 & 225 \\
 63 & 280 & 287 \\
 65 & 72 & 97 \\
 65 & 156 & 169 \\
 68 & 285 & 293 \\
 69 & 92 & 115 \\
 69 & 260 & 269 \\
 72 & 135 & 153 \\
 75 & 100 & 125 \\
 75 & 180 & 195 \\
 77 & 264 & 275 \\
 81 & 108 & 135 \\
 84 & 135 & 159 \\
 84 & 187 & 205 \\
 84 & 245 & 259 \\
 85 & 132 & 157 \\
 85 & 204 & 221 \\
 87 & 116 & 145 \\
 88 & 105 & 137 \\
 88 & 165 & 187 \\
 93 & 124 & 155 \\
 95 & 168 & 193 \\
 95 & 228 & 247 \\
 96 & 247 & 265 \\
 99 & 132 & 165 \\
 99 & 168 & 195 \\
 100 & 105 & 145 \\
 104 & 153 & 185 \\
 104 & 195 & 221 \\
 105 & 140 & 175 \\
 105 & 208 & 233 \\
 105 & 252 & 273 \\
 108 & 231 & 255 \\
 111 & 148 & 185 \\
 115 & 252 & 277 \\
 115 & 276 & 299 \\
 117 & 156 & 195 \\
 117 & 240 & 267 \\
 119 & 120 & 169 \\
 120 & 209 & 241 \\
 120 & 225 & 255 \\
 123 & 164 & 205 \\
 125 & 300 & 325 \\
 129 & 172 & 215 \\
 133 & 156 & 205 \\
 135 & 180 & 225 \\
 136 & 255 & 289 \\
 136 & 273 & 305 \\
 140 & 147 & 203 \\
 140 & 171 & 221 \\
 140 & 225 & 265 \\
 141 & 188 & 235 \\
 144 & 165 & 219 \\
 147 & 196 & 245 \\
 152 & 285 & 323 \\
 153 & 204 & 255 \\
 159 & 212 & 265 \\
 160 & 231 & 281 \\
 161 & 240 & 289 \\
 165 & 220 & 275 \\
 165 & 280 & 325 \\
 171 & 228 & 285 \\
 175 & 288 & 337 \\
 177 & 236 & 295 \\
 180 & 189 & 261 \\
 180 & 273 & 327 \\
 180 & 299 & 349 \\
 183 & 244 & 305 \\
 189 & 252 & 315 \\
 195 & 216 & 291 \\
 195 & 260 & 325 \\
 201 & 268 & 335 \\
 204 & 253 & 325 \\
 207 & 224 & 305 \\
 207 & 276 & 345 \\
 213 & 284 & 355 \\
 219 & 292 & 365 \\
 220 & 231 & 319 \\
 225 & 272 & 353 \\
 225 & 300 & 375 \\
 240 & 275 & 365 \\
 252 & 275 & 373 \\
 260 & 273 & 377 \\
 6 & 8 & 10 \\
 10 & 24 & 26 \\
 12 & 16 & 20 \\
 14 & 48 & 50 \\
 16 & 30 & 34 \\
 18 & 24 & 30 \\
 18 & 80 & 82 \\
 20 & 48 & 52 \\
 22 & 120 & 122 \\
 24 & 32 & 40 \\
 24 & 70 & 74 \\
 26 & 168 & 170 \\
 28 & 96 & 100 \\
 30 & 40 & 50 \\
 30 & 72 & 78 \\
 30 & 224 & 226 \\
 32 & 60 & 68 \\
 32 & 126 & 130 \\
 34 & 288 & 290 \\
 36 & 48 & 60 \\
 36 & 160 & 164 \\
 40 & 42 & 58 \\
 40 & 96 & 104 \\
 40 & 198 & 202 \\
 42 & 56 & 70 \\
 42 & 144 & 150 \\
 44 & 240 & 244 \\
 48 & 64 & 80 \\
 48 & 90 & 102 \\
 48 & 140 & 148 \\
 48 & 286 & 290 \\
 50 & 120 & 130 \\
 54 & 72 & 90 \\
 54 & 240 & 246 \\
 56 & 90 & 106 \\
 56 & 192 & 200 \\
 60 & 80 & 100 \\
 60 & 144 & 156 \\
 64 & 120 & 136 \\
 64 & 252 & 260 \\
 66 & 88 & 110 \\
 66 & 112 & 130 \\
 70 & 168 & 182 \\
 70 & 240 & 250 \\
 72 & 96 & 120 \\
 72 & 154 & 170 \\
 72 & 210 & 222 \\
 78 & 104 & 130 \\
 78 & 160 & 178 \\
 80 & 84 & 116 \\
 80 & 150 & 170 \\
 80 & 192 & 208 \\
 84 & 112 & 140 \\
 84 & 288 & 300 \\
 88 & 234 & 250 \\
 90 & 120 & 150 \\
 90 & 216 & 234 \\
 96 & 110 & 146 \\
 96 & 128 & 160 \\
 96 & 180 & 204 \\
 96 & 280 & 296 \\
 100 & 240 & 260 \\
 102 & 136 & 170 \\
 102 & 280 & 298 \\
 108 & 144 & 180 \\
 110 & 264 & 286 \\
 112 & 180 & 212 \\
 112 & 210 & 238 \\
 114 & 152 & 190 \\
 120 & 126 & 174 \\
 120 & 160 & 200 \\
 120 & 182 & 218 \\
 120 & 288 & 312 \\
 126 & 168 & 210 \\
 128 & 240 & 272 \\
 130 & 144 & 194 \\
 132 & 176 & 220 \\
 132 & 224 & 260 \\
 138 & 184 & 230 \\
 144 & 192 & 240 \\
 144 & 270 & 306 \\
 150 & 200 & 250 \\
 156 & 208 & 260 \\
 160 & 168 & 232 \\
 160 & 300 & 340 \\
 162 & 216 & 270 \\
 168 & 224 & 280 \\
 168 & 270 & 318 \\
 170 & 264 & 314 \\
 174 & 232 & 290 \\
 176 & 210 & 274 \\
 180 & 240 & 300 \\
 186 & 248 & 310 \\
 192 & 220 & 292 \\
 192 & 256 & 320 \\
 198 & 264 & 330 \\
 200 & 210 & 290 \\
 204 & 272 & 340 \\
 210 & 280 & 350 \\
 216 & 288 & 360 \\
 222 & 296 & 370 \\
 238 & 240 & 338 \\
 240 & 252 & 348 \\
 260 & 288 & 388 \\
 280 & 294 & 406 \\
\end{array}
\right)

Best Answer

Here is my attempt to generate the triples (edit: and the number of triples less than):

\documentclass{article}
\usepackage[margin=3cm]{geometry}
\usepackage{xcolor}
\makeatletter
\newcount\coeff@u
\newcount\coeff@v
\newcount\gcd@a
\newcount\gcd@b
\newcount\cnt@triples
\newif\if@count@triples

\newcommand*\countpytha{\pytha@i\@count@triplestrue}
\newcommand*\pytha[1]{%
    \par\noindent
    \pytha@i\@count@triplesfalse{#1}%
    \par
}
\newcommand*\pytha@i[2]{%
    \def\pytha@max{#2}\coeff@u\@ne\coeff@v\@ne
    \begingroup
        #1\fboxsep2pt
        \pytha@ii
    \endgroup
}
\newcommand*\pytha@ii{%
    \ifnum\coeff@v<\coeff@u
        \advance\coeff@v\@ne
    \else
        \coeff@v\@ne
        \advance\coeff@u\@ne
    \fi
    \let\pytha@next\pytha@ii
    \ifodd\numexpr\coeff@v-\coeff@u\relax
        \edef\num@c{\number\numexpr\coeff@u*\coeff@u+\coeff@v*\coeff@v\relax}%
        \ifnum\num@c>\pytha@max\relax
            \ifnum\coeff@v<3
                \if@count@triples\def\pytha@next{\the\cnt@triples}%
            \else
                \let\pytha@next\relax
            \fi
        \fi
        \else
            \calc@gcd\coeff@u\coeff@v
            \ifnum\gcd@b=\@ne
                \edef\num@a{\number\numexpr\coeff@u*\coeff@u-\coeff@v*\coeff@v\relax}%
                \edef\num@b{\number\numexpr2*\coeff@u*\coeff@v}%
                \ifnum\numexpr\num@a*\num@a+\num@b*\num@b-\num@c*\num@c\relax=\z@
                    \if@count@triples
                        \advance\cnt@triples\@ne
                    \else
                        \colorbox{blue!20}{%
                            \hbox to\dimexpr(\linewidth-10\fboxsep)/5-1pt{\hss(\min@oftwo\num@a\num@b,\max@oftwo\num@a\num@b,\num@c)\hss}%
                        }%
                        \hskip1pt \penalty-50
                    \fi
                \fi
            \fi
        \fi
    \fi
    \pytha@next
}
\newcommand\calc@gcd[2]{%
    \gcd@a\max@oftwo{#1}{#2}%
    \gcd@b\min@oftwo{#1}{#2}%
    \calc@gcd@i
}
\newcommand*\calc@gcd@i{%
    \edef\gcd@tmp{\number\gcd@a}%
    \divide\gcd@a\gcd@b
    \edef\gcd@tmp{\number\numexpr\gcd@tmp-\gcd@b*\gcd@a}%
    \unless\ifnum\gcd@tmp=\z@
        \gcd@a\gcd@b
        \gcd@b\gcd@tmp\relax
        \expandafter\calc@gcd@i
    \fi
}
\newcommand\min@oftwo[2]{\ifnum\numexpr#1-#2\relax<\z@#1\else#2\fi}
\newcommand\max@oftwo[2]{\ifnum\numexpr#1-#2\relax<\z@#2\else#1\fi}
\makeatother
\begin{document}
Here is the \countpytha{1000} triples less than 1000 :
\pytha{1000}
\end{document}
Related Question