13

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)
11
  • 4
    It is known that primitive Pythagorean triples are obtained with $(u^2-v^2,2uv,u^2+v^2)$, where $u$ and $v$ are coprime positive integers, one odd and one even.
    – egreg
    Sep 22, 2013 at 10:38
  • 7
    Please show us what you have tried so far. If you are asking about an algorithm to generate such triples then that is off topic. Sep 22, 2013 at 10:39
  • 3
    Why are you trying to do this using TeX? Do you have a particular goal that makes this choice preferable?
    – long tom
    Sep 22, 2013 at 11:33
  • 2
    Yeah, just write it in python or something and make it output a TeX table fragment. Or use LuaLaTeX. Everything else is just unnecessary pain. Possible of course but unnecessarily painful.
    – Christian
    Sep 22, 2013 at 12:19
  • 1
    So your question is how to format the result you already got differently? Well, it would be best if you modified the output in Mathematica since you already got everything in place there. There is a mathematica.stackexchange.com so best ask there how to do that.
    – Christian
    Sep 22, 2013 at 13:52

5 Answers 5

7

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}
2
  • Possible improvement: Automatically calculate the number of triples with c < #1. Sep 23, 2013 at 14:25
  • You can also ask me, if you think I am good enough at latex coding... See my edit.
    – unbonpetit
    Sep 24, 2013 at 10:33
15

You can even use the Mathematica Output:

\documentclass{article}
\usepackage{xparse,xcolor}
\ExplSyntaxOn
\NewDocumentCommand{\pythtriples}{m}
 {
  \begin{flushleft}
  \setlength{\fboxsep}{0pt} % \colorbox doesn't add to the width
  \setlength{\lineskiplimit}{\maxdimen} % all lines are too near
  \setlength{\lineskip}{1pt} % it's the default, but makes no harm
  % five boxes per line
  \dim_set:Nn \l__pyth_width_dim { (\linewidth-4pt)/5 }
  % do a mapping on all terms of the input
  \clist_map_inline:nn { #1 }
   {
    \colorbox{blue!20}
     {
      \strut
      % five columns with 1pt separation
      \makebox[\l__pyth_width_dim]{$(##1)$}
     }
    \hspace{1pt plus 0.1pt minus 0.1pt}
   }
  \end{flushleft}
 }
\dim_new:N \l__pyth_width_dim
\ExplSyntaxOff

\begin{document}
Here are some primitive Pythagorean triples:
\pythtriples{
  {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}
}
Of course, the list is infinite.

\end{document}

enter image description here

One can use it also for different formatting; just choose in a suitable way the mapping function.

3
  • If I use the command {x, y, z} /. Solve[{x^2 + y^2 == z^2, 1 <= x <= 5000, 1 <= y <= 5000, x < y, z > 0}, {x, y, z}, Integers] in Mathematica, then your table does not fit with Mathematica out put. Sep 24, 2013 at 15:25
  • @ egreg Please format for me this out put from Mathematica \pythtriples{{{-3, 0, -2}, {6, -4, -1}, {0, -4, -1}}, {{-3, 0, -2}, {9, 0, -2}, {0, -4, -1}}, {{-3, 4, -2}, {-3, 4, 4}, {0, 4, 7}}, {{-3, 4, -2}, {0, 4, -5}, {0, 4, 7}}, {{-3, 4, -2}, {6, 4, -5}, {0, 4, 7}}} Nov 16, 2013 at 4:33
  • @minthao_2011 This is not in the requested format: you have surplus braces. And I don't see how those triples can be interpreted as solutions of the problem.
    – egreg
    Nov 16, 2013 at 10:42
9

The following example puts the triples in paragraph mode using boxes (\colorbox) with light gray background. Some voodoo ensures that the lines are correctly filled and that the boxes have equal distances (\triplesep) in both horizontal and vertical direction.

\documentclass{article}
\usepackage[a4paper, vmargin=0mm]{geometry}

\usepackage{xcolor}
\definecolor{triplebackground}{gray}{.8}
\newdimen\triplewidth
\newlength\triplesep
\setlength{\triplesep}{1pt}
\newenvironment{triples}[1]{%
  \par
  \setlength{\parindent}{0pt}%
  \setlength{\baselineskip}{0pt}%
  \setlength{\lineskip}{\triplesep}%
  \setlength{\leftskip}{-.5\triplesep plus 1pt}%
  \setlength{\rightskip}{-.5\triplesep plus 1pt}%
  \setlength{\triplewidth}{%
    \dimexpr(\linewidth-\numexpr(#1)-1\relax\triplesep)/(#1)\relax
  }%
  \newcommand*{\triple}[1]{%
    \leavevmode
    \hspace*{.5\triplesep}%
    \colorbox{triplebackground}{%
      \hbox to \dimexpr\triplewidth-2\fboxsep{\hfill$\mathsf{(##1)}$\hfill}%
    }%
    \kern.5\triplesep
    \penalty100 %
    \ignorespaces
  }%
}{\par}

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

Result

4

Here is another formatting attempt which is based on using a tabular (which should be replaced by a longtable in some situations).

An environment creates the tabular with the given number of columns and arbitrary column specifier. Optionally, a further macro passed as option to the environment deals with each individual cell. In this example this macro sets the cell width so that the tabular occupies (circa) 80% of the line width.

Of course, this is to be applied to lists as here where each "value" is within braces...

\documentclass{article}
\usepackage{array}
\usepackage{xcolor}

\makeatletter

% The TableFromList environment converts a comma separated list 
% of *braced* data into a tabular with a given # of columns and a given
% column specifier. (it will crash if the list ends with a comma)

% TableFromList has two mandatory argument and an optional one
% The first mandatory is the number of column
% The second mandatory is the column specifier for tabular

% The optional macro must be a two parameter macro: it will receive as
% first argument the number of columns, as second argument the cell
% contents. 

\newenvironment{TableFromList}[3][\@secondoftwo]
          {\centering\begin{tabular}{*{#2}{#3}}
                     \converttotable@ {#2}{#1}{#2}}{}

\def\converttotable@ #1#2#3#4#5%
{%
    #2{#3}{#4}\ifx#5,% 
            \ifnum #1>1
              \expandafter\expandafter\expandafter\@firstoftwo
            \else
              \expandafter\expandafter\expandafter\@secondoftwo
            \fi
         \else
           \expandafter \converttotable@@ \expandafter #5%
         \fi         
         {&\expandafter\converttotable@\expandafter{\the\numexpr #1-1}{#2}} 
         {\\\converttotable@ {#3}{#2}}{#3}%
}%
\def\converttotable@@ #1#2#3#4{\end{tabular}\par #1}

\makeatother

% An example of macro dealin with the cell contents
\definecolor{triplebackground}{gray}{.8}

\newcommand{\PrettyTriple}[3][.4pt]
    {\setlength{\fboxsep}{#1}%
     \colorbox{white}{\colorbox{triplebackground}
                      {\makebox [\dimexpr.8\linewidth/#2]{\strut\textsf{#3}}}}}


\begin{document}

% no Cell Decoration: (but could be added to the specifier)
% \begin{TableFromList}{5}{c}
%    {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} 
% \end{TableFromList}

\begin{TableFromList}[\PrettyTriple]{4}{@{}c@{}}
   {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}
\end{TableFromList}

\begin{TableFromList}[{\PrettyTriple [2pt]}]{5}{@{}c@{}}
   {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}
\end{TableFromList}

\end{document}

Full table, 4 cells per row (centered in the original:)

triples

Second, smaller table with a larger intra-row space and 5 cells per row:

small triples

2
  • +1 but still disappointed as I think you will generate the prime triples with TeX. :-) Sep 23, 2013 at 15:42
  • 2
    @PGFTricks of course piece of cake with xint if you want it expandably :-) but the OP didn't seem to want that, and besides there is a nice answer doing it already...
    – user4686
    Sep 23, 2013 at 15:43
2

The primitive Pythagorean triples a^2+b^2=c^2, a>0, b>0, are generated by the formulas:

a=m^2-n^2, b=2mn, c=m^2+n^2, with m > n > 0 having no common factor, and one of them is even.

(this gives the (a,b,c)'s with b even, hence necessarily a odd.)

a=2mn, b=m^2-n^2, c=m^2+n^2, with m > n > 0 having no common factor, and one of them is even.

(this gives the (a,b,c)'s with a even, hence necessarily b odd.)

Naturally, each member of the first set uniquely defines a member of the second set by exchanging a and b.

Notice though that this does not immediately produce the triples (a,b,c) with the natural condition 0<a<b, thus we can use this:

a=min(m^2-n^2,2mn), b=max(m^2-n^2,2mn), m>n>0 together primes, one of them even.

The code below (plain TeX, but compiles identically with LaTeX) simply generates all candidate couples (m,n) of integers of opposite parities, and eliminates those having a common factor (using \xintGCD from the xintgcd package).

This updates arranges the (a,b,c) triples to verify a<b.

Nota bene: there are ways to generate the primitive pythagorean triples a^2+b^2=c^2, a>0, b>0, without having to check that some numbers have no common factor, either via the parametrization above or more elegantly using the formulas from Tree of pythagorean triples on wikipedia. The method as presented on that page would need some additional investigations however if the requirement a<b is added.


edit 2015/08/30: I have added \input xint.sty to the start of the code, because I realize today that some macro arising in the expansion of \xintGCD is (since release 1.1 of 2014/10/28 -- sigh...) lacking from xintgcd.sty. This bug affects xint versions 1.1 and 1.1a and will be fixed in future releases. In earlier releases xintgcd loaded xint, it now loads a only subset called xintcore, and this subset is thus lacking some auxiliary macros which were left in xint.sty and not transferred.


\input xint.sty % needed with xint 1.1a
\input xintgcd.sty

\newcount\cntn % will hold n
\newcount\cntm % will hold m
\newcount\nbtriples % will count the number of triples generated
\newtoks\Triples


% the \loop .. \repeat of plain tex can not be nested
% we make a clone in order to nest to one level. The \loop of latex
% is a bit different, but not any more nestable than the Plain one.

\def\LOOP #1\REPEAT{\def \BODY {#1}\ITERATE } 
\def\ITERATE{\BODY \let \next \ITERATE \else \let \next \relax \fi \next }

% \def\gobble#1{}

% use of \edef etc in \AddNewTriple is a bit sub-optimal, but let's forget about it. 

\def\OrderAandB #1,#2,{\ifnum #1>#2 #2, #1,\else #1, #2,\fi }

\def\EuclideFormula {\expandafter\OrderAandB
                     \the\numexpr\cntm*\cntm-\cntn*\cntn\expandafter,%
                 \the\numexpr 2*\cntm*\cntn,
                 \the\numexpr\cntm*\cntm+\cntn*\cntn }

\def\AddNewTriple 
          {\edef\tmp {, (\EuclideFormula)}%
           \advance\nbtriples 1
           \Triples\expandafter\expandafter\expandafter
                 {\expandafter\the\expandafter\Triples\tmp}}

\def\generatetriples #1{%
    \Triples{(3, 4, 5)}%
    \nbtriples 1
    \cntm 3
    \LOOP
        % M is ODD, N MUST BE EVEN
        \cntn 0
        \loop
          \advance\cntn 2 
        \ifnum\cntn<\cntm
            \ifcase\xintGCD{\cntm}{\cntn} 
            \or 
              \AddNewTriple % gcd(m,n)=1, primitive triple
            \fi
        \repeat
        \advance\cntm 1
        % M iS EVEN, N MUST BE ODD
        \cntn 1 
        \AddNewTriple
        \loop
          \advance\cntn 2
        \ifnum\cntn<\cntm
            \ifcase\xintGCD{\cntm}{\cntn} 
            \or 
              \AddNewTriple % gcd(m,n)=1, primitive triple
            \fi
        \repeat
    \advance\cntm 1
    %%% \Triples\expandafter{\the\Triples\expandafter\endgraf\gobble }%
    \ifnum#1>\cntm
    \REPEAT
}

\generatetriples {60}

We have generated \the\nbtriples{} primitive Triples. Here they are:\par
\the\Triples
%%% or \the\Triples, if the \endgraf line above is de-commented-out.     


\bye

Here is the first page of output. The formatting issues have been addressed in the other answers.

pythagorean triples

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .