add tag
samcarter
> This is part of the Summer of Code 2022 series, see https://topanswers.xyz/tex?q=2059 for more information

If p is the perimeter of a right angle triangle with integral length sides, {a,b,c}, there are exactly three solutions for p = 120.

{20,48,52}, {24,45,51}, {30,40,50}

For which value of p ≤ 1000, is the number of solutions maximised?

(this programming puzzle is taken from https://projecteuler.net/problem=39, licensed under CC BY-NC-SA 4.0)

![SoC.png](/image?hash=c1aa67212c8c9bfd06d1dbb6d0959bbbb217d5e91e509f70ba462a103709f70b)
Top Answer
samcarter
*No spoiler*

Expanding the solution to SoC Day 1 a bit gives an inefficient but working solution:


```
% !TeX TS-program = pdflatex

\documentclass{article}

\begin{document}

If p is the perimeter of a right angle triangle with integral length sides, $\{a,b,c\}$, there are exactly three solutions for $p = 120$.

\[
\{20,48,52\}, \{24,45,51\}, \{30,40,50\}
\]

For which value of $p \leq 1000$, is the number of solutions maximised?

\ExplSyntaxOn

\int_new:N \l_sam_a_int
\int_new:N \l_sam_b_int

\int_new:N \l_sam_p_int
\int_new:N \l_sam_count_int

\int_new:N \l_sam_tot_int
\int_new:N \l_sam_tot_p_int


\int_step_inline:nn { 1000 }
{ 

  \int_set:Nn \l_sam_p_int { #1 }
  \int_zero:N \l_sam_count_int
  
  % loop over b (next biggest integer)
  \int_step_inline:nn {\l_sam_p_int}
  { 
  
    \int_set:Nn \l_sam_b_int { ##1 }
  
    % loop over a (smalles integer)
    \int_step_inline:nn {\l_sam_b_int-1} 
    {
    
      \int_set:Nn \l_sam_a_int { ####1 } 
    
      % check if a^2 + b^2 = (p - a - b)^2
      \int_compare:nNnT 
      {
        \l_sam_b_int*\l_sam_b_int + \l_sam_a_int*\l_sam_a_int 
      } = { 
        (\l_sam_p_int-\l_sam_a_int-\l_sam_b_int)*(\l_sam_p_int-\l_sam_a_int-\l_sam_b_int) 
      }{      
        \int_incr:N \l_sam_count_int
      }% if a^2 + b^2 = (p - a - b)^2
  
    }% loop a   
  }% loop b
  
  \par \int_use:N \l_sam_p_int :~ \int_use:N \l_sam_count_int
  
  \int_compare:nNnT { \l_sam_count_int } > { \l_sam_tot_int }
  {
    \int_set:Nn \l_sam_tot_int { \l_sam_count_int } 
    \int_set:Nn \l_sam_tot_p_int { \l_sam_p_int } 
  }
  
}

\par Maximum~ solution~ for~ $p = \int_use:N \l_sam_tot_p_int$:~
\int_use:N \l_sam_tot_int \nobreakspace solutions.
\ExplSyntaxOff

\end{document}
```
Answer #2
CrazyHorse
without sorting the values and using `lualatex`

```
\documentclass{article}
\usepackage{luacode}
\begin{luacode}
  function triangle(p)
	tex.print("Triangle Numbers for "..tostring(p).."\\par")
	local found = false
	for b = 1, math.floor(p/3)-1 do
      for a = 1, p-b do
        if (a*a+b*b) == (p-a-b)^2 then
          c = p - a - b
          tex.print("("..tostring(a)..","..tostring(b)..","..tostring(c).."), ")
          found = true
        end
	  end
	end
	if not found then tex.print("---") end
  end
\end{luacode}
\def\triangle#1{\directlua{triangle(#1)}}

\begin{document}
	\triangle{120}\par	
	\triangle{200}\par	
	\triangle{500}\par	
	\triangle{1000}	
\end{document}
```

![Bildschirmfoto 2022-08-28 um 21.55.09.png](/image?hash=af5ab5f4530e4e610b3ecf99d4f35b6d33d71a7b4ad453df51a76466038268f3)

Enter question or answer id or url (and optionally further answer ids/urls from the same question) from

Separate each id/url with a space. No need to list your own answers; they will be imported automatically.