How to write a code to find the Pythagorean Triples

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.


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


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}

Tags:

Code

Tables