# Generating Pythagorean triples where the legs are Hypotenuses of other Pythagorean triples

I know how to generate regular Pythagorean Triples given two positive integers P and Q such that $$a=2*p*q$$ $$b=p^2-q^2$$ $$c=p^2+q^2$$ where $$p>q$$, but I want to find scenarios where $$a$$ and $$b$$ can also be written in the format $$p^2+q^2$$. Specifically, I am looking for a generalized way of finding all situations where the hypotenuses of the first two triples form the legs of the third triple.  I want to be able to generate a formula for finding these special triples.

So far, I have been able to find four cases where this holds true by using the brute force method.

$$P_1$$ $$Q_1$$ $$A_1=2P_1Q_1$$ $$B_1=P_1^2-Q_1^2$$ $$C_1=A_3$$
10 2 40 96 104
16 4 128 240 272
15 9 270 144 306
21 3 126 432 450
$$P_2$$ $$Q_2$$ $$A_2=2P_2Q_2$$ $$B_2=P_2^2-Q_2^2$$ $$C_2=B_3$$
12 3 72 135 153
12 9 216 63 225
12 8 192 80 208
20 12 480 256 544
$$P_3$$ $$Q_3$$ $$A_3=2P_3Q_3=C_1$$ $$B_3=P_3^2-Q_3^2=C_2$$ $$C_3=C_1^2+C_2^2$$
13 4 104 153 185
17 8 272 225 353
17 9 306 208 370
25 9 450 544 706

As you can see, the third chart shows Pythagorean triples with legs that are also hypotenuses of the previous charts. For example, the right triangle with sides (104, 153,185) corresponds to the right triangles with (40,96,104) and (72,135,153). The P and Q vales show $$P_1^2+Q_1^2=2P_3Q_3$$ ($$10^2+2^2=2*13*4=104$$), and $$P_2^2+Q_2^2=P_3^2-Q_3^2$$ ($$12^2+3^2=13^2-4^2=153$$).

I gathered this information using the highly inefficient and laborious method of inputting a table of P and Q values into Google sheets up to P and Q being less than or equal to 25. Then, I searched the data by using vlookup and countif functions, after sorting it a bit.

 I think there must be a way of simplifying this process, but it eludes me. How can I (in a much less time consuming way) generate these special Pythagorean triples where each side of the triangle is a hypotenuse of another Pythagorean triple?

Edit: While the Euclidean formula for Pythagorean triples relies on the value of $$p$$ and $$q$$ to generate legs $$a$$ and $$b$$ with hypotenuse $$c$$, I am looking for a generalized way of finding the special "triple triples" for lack of a better term given integer inputs $$(input_1...input_k)$$ that I can input into functions $$f_A(input_1...input_k)=A$$ $$f_B(input_1...input_k)=B$$ $$f_C(input_1...input_k)=C$$ Where A and B are hypotenuses of other Pythagorean triples, and (A,B,C) forms a Pythagorean triple in and of itself.

• What is your ultimate goal? You can write a program to generate millions of these. Anyway see sum of two squares for the information you need. May 10 at 4:14
• The positive integers being the sum of at most two squares can easily be classified, but when a positive integer is the sum of two positive squares is a bit more complicated. At least you can check the necessary condition to find out whether $a,b$ can be the sum of two squares at all. One additional fact you can use is that one of $a,b$ must be divisible by $3$ and therefore divisible by $9$. May 10 at 8:07
• Ultimately, I am looking for a method of generating the sides of the special Pythagorean triple, given certain input variables. I want it to function in a similar way to Euclid's formula for regular Pythagorean triples (input P and Q to yield A,B, and C) but I'm not sure if I would need more than 2 inputs, or if this approach is even viable. May 10 at 8:58
• If you want "three Pythagorean triples where the hypotenuses of the first two form the legs of the third", then you need $$p_1^2 + q_1^2 = h_1^2 \\p_2^2 + q_2^2 = h_2^2\\h_1^2 + h_2^2 = h_3^2$$ Combining gives $$p_1^2 + q_1^2 + p_2^2 + q_2^2= h_3^2$$ without any nested squaring. But that equation itself does not require the $p_i, q_i$ to be parts of Pythagorean triples, It is unclear to me if you are interested in solving the Pythagorean triple problem described, or your nested-squares problem, but the two are not the same. May 10 at 15:36
• I guess looking at your table, you really do want solve the Pythagorean triple problem. But be aware that your nested squared equation is something different. Also note that the $P_i, Q_i$ in your table do not play the roles of $p_i, q_i$ in your description of the problem. Instead it is the $A_i, B_i$ that correspond to $p_i, q_i$. This inconsistent labelling makes it confusing to work out what you are doing. May 10 at 15:51

An alternative to using the new formula in my other answer uses Euclid's formula –– the "new" formula would work as well, and the search would be at least twice as fast, but the Euclid's formula is simpler –– to find a candidate C-value, find triple with a matching even leg, and then test to see if the odd leg(s) is/are also valid. Here, Euclid's formula is show as: $$A=m^2-k^2\qquad B=2mk\qquad C=m^2+n^2$$

All "candidate" C-values will have $$\space 16(n)+4, n\in\mathbb{N}\space$$ as a factor. There are no valid C-values for $$\space n\in\{1,2\}\space$$ but there is for $$\space16(3)+4=52.\quad$$ To find a triple for this or any other candidate, we solve the C-funciton for $$\space k\space$$ and test a defined range of $$\space m$$-values to see which, if any yield integers. $$C=m^2+k^2\implies k=\sqrt{C-m^2}\\ \qquad\text{for}\qquad \bigg\lfloor\frac{ 1+\sqrt{2C-1}}{2}\bigg\rfloor \le m \le \lfloor\sqrt{C-1}\rfloor$$ The lower limit ensures $$m>k$$ and the upper limit ensures $$k\in\mathbb{N}$$. $$C=52\implies \bigg\lfloor\frac{ 1+\sqrt{108-1}}{2}\bigg\rfloor=5 \le m \le \lfloor\sqrt{52-1}\rfloor=7\quad \\ \text{and we find} \quad m\in\{6\}\Rightarrow k\in\{4\}\\$$ $$f(6,4)=(20,48,52)=4\times(5,12,13)$$

Since $$\space52=4\times13\space$$ (two distinct prime factors) there are $$\space 2^{2-1}=2\space$$ primitive triples where $$\space B=52.\quad$$ To find them, we solve the B-function for $$\space k\space$$ and test a defined range of $$\space m$$-values to see which yield integers.

$$B=2mk\implies k=\frac{B}{2m}\qquad\text{for}\qquad \bigg\lfloor \frac{1+\sqrt{2B+1}}{2}\bigg\rfloor \le m \le \frac{B}{2}$$ The lower limit ensures $$m>k$$ and the upper limit ensures $$m>1$$ $$B=52\implies\qquad \bigg\lfloor \frac{1+\sqrt{105+1}}{2}\bigg\rfloor =5 \le m \le \frac{52}{2}=26\\ \text{and we find} \quad m\in\{13,26\}\implies k\in\{2,1\}$$ $$f(13,2)=(165,52,173)\space GCD(1)\quad f(26,1)=(675,52,677)\space GCD(1)$$ Now we test the values $$\space 165\space$$ and $$\space 675\space$$ and find no triple using the $$C$$-search method above. Notice, however that both are divisible by $$\space 5\space$$ and $$\space(3,4,5)\space$$ is a valid triple so we have $$f(2,1)=(3,4,5)\longrightarrow (99,132,165)\space GCD(33)\\ f(2,1)=(3,4,5)\longrightarrow(405,540,675)\space GCD(135)$$

We found three hypotenuse values that match both legs of two other triples.

Using this method, we can easily find other sets of $$\space3$$-or-more triples where both legs are valid C-values:

$$16(4)+4=68\\ f(8,2)=(60,32,68)\quad GCD(4)\\ f(17,2)=(285,68,293)\quad GCD(1)\\ f(2,1)=(3,4,5)==> (171,228,285)\space GCD(57)$$

$$16(6)+4=100\\ f(8,6)=(28,96,100)\quad GCD(4)\\ f(10,5)=(75,100,125)\quad GCD(25)\\ f(2,1)=(3,4,5)==> (45,60,75)\space GCD(15)$$

$$8(5,12,13)=2(20,48,52)=(40,96,104)\\ f(10,2)=(96,40,104)\quad GCD(8)\\ f(13,4)=(153,104,185)\quad GCD(1)\\ f(12,3)=(135,72,153)$$

Note in the last example how $$\space 104=2^3\times 13.\quad$$We can always find valid starting "candidate" values by using $$\space C=2^{x+1}\times h,\space x\in\mathbb{N}\space$$ where $$\space h\space$$ is any value in the series shown here.

$$\large\textbf{Update: }\space$$ These $$\space h$$-values and more can be found using a variant of Euclid's formula that replaces $$\space m,k\space$$ with $$\big((2n-1+k),k\big), n,k\in\mathbb{N}.$$ \begin{align*} A=&(2n-1)^2+ &2(2n-1)k\\ B=& &2(2n-1)k &+2k^2\\ C=&(2n-1)^2+ &2(2n-1)k &+2k^2\\ \end{align*}

If the $$\space 4\times h$$-value does not work, we can try $$\space 4\times h^2\space$$ or $$\space h^3\space$$ etc. Using the new formula,

\begin{align} F(1,1)&=(3,4,5)\\ 4\times5^2&\longrightarrow(28,96,100)\\ 100&\longrightarrow(75,100,125)\\ &\longrightarrow(621,100,629)\\ &\longrightarrow(2499,100,2501)\\ 75&\longrightarrow 3(7,24,25)=(21,72,75)\\ 621&\longrightarrow\text{nothing}\\ 2499&\longrightarrow 147(15,8,17) =(2205,1176,2499)\\ \end{align}

\begin{align} F(1,1)&=(3,4,5)\\ 5^3&\longrightarrow(75,100,125)\\ 125&\longrightarrow(125,300,325)\\ &\longrightarrow(125,7812,7813)\\ 300&\longrightarrow 60(3,4,5)=(180,240,300)\\ &\longrightarrow 12(7,24,25)=(84,288,300)\\ 7812&\longrightarrow \text{nothing}\\ \end{align}

For the hypotenuse of any primitive Pythagorean triple, there are $$\space 2^{x-1} \space$$ primitive triples with one leg equal to that hypotenuse where $$\space x\space$$ is the number of distinct prime factors of that hypotenuse.

We will discuss only primitive Pythagorean triples because they are the interesting ones –– imprimitives are just multiples of primitives. With primitives, one leg and the hypotenuse are odd and the other leg is even. For this discussion, we will show Euclid's formula as $$\space A=m^2-k^2 \quad B=2mk\quad C=m^2+k^2\quad$$ and we will use a variant where $$\space m=2n-1+k.\quad$$ The new formula generates only and all primitives where $$\space GCD(A,B,C)=(2x-1)^2,\space$$ which includes all primitives. This formula is:

\begin{align*} A=&(2n-1)^2+ &2(2n-1)k\\ B=& &2(2n-1)k &+2k^2\\ C=&(2n-1)^2+ &2(2n-1)k &+2k^2\\ \end{align*} and produces this array of mostly-primitive Pythagorean triples

$$\begin{array}{c|c|c|c|c|c|c|} n & k=1 & k=2 & k=3 & k=4 & k=5\\ \hline Set_1 & 3,4,5 & 5,12,13& 7,24,25& 9,40,41& 11,60,61\\ \hline Set_2 & 15,8,17 & 21,20,29 &27,36,45 &33,56,65 & 39,80,89\\ \hline Set_3 & 35,12,37 & 45,28,53 &55,48,73 &65,72,97 & 75,100,125\\ \hline Set_{4} &63,16,65 &77,36,85 &91,60,109 &105,88,137 &119,120,169 \\ \hline Set_{5} &99,20,101 &117,44,125 &135,72,153 &153,104,185 &171,140,221\\ \hline \end{array}$$ Note that side_A of $$\space Set_1\space$$ includes all odd numbers greater than one so it also contains all primitive hypotenuse values –– which are of the form $$\space 4x+1.\space$$ Other sets also have valid hypotenuse values for side-A and we will see how to find them all.

It is possible for a hypotenuse to be side-B of a triple but only if the hypotenuse $$\space C \space$$ is a multiple of $$\space 4, \space$$ i.e. from a non-primitive triple. We solve the B-function for $$\space k \space$$ and test a defined range of $$\space n$$-values to see which yield integers.

We will use $$\space C=104=4\times26\space$$ to demonstrate:

$$B=2(2n-1)k+2k^2\implies k=\frac{\sqrt{2B+(2n-1)^2)}-(2n-1)}{2} \\\text{for}\quad1\le n \le\frac{B}{4}$$

$$B=104 \implies 1\le n\le\frac{104}{4} =26 \\\text{and we find}\quad n\in \{5,26\} \implies k\in \{4,1\}$$

$$f(5,4)=(153,104,185)\qquad f(26,1)=(2703,104,2705)$$

We now solve the $$\space A$$-funcion for $$\space k \space$$ and test a defined range of $$\space n$$-values to see which yield integers.

$$A=(2n-1)^2+2(2n-1)k\implies k=\frac{A-(2n-1)^2}{2(2n-1)} \\\text{for}\quad 1\le n\le\biggl\lfloor\frac{\sqrt{A+1}}{2}\biggr\rfloor$$

For example $$A=153\implies 1\le n\le \bigg\lfloor\frac{\sqrt{153+1}}{2}\bigg\rfloor=77 \\\text{and we find}\quad n \in \{1,2,5\} \implies k\in\{76,24,4\}$$ $$f(1,76)=(153,11704,11705)\qquad f(2,24)=(153,1296,1305)\\ f(5,4)=(153,104,185)\\$$

Note that $$\space 153=3^2\times17\space$$ so there are $$\space 2^{2-1}=2\space$$ primitive triples where $$\space A=153.$$

$$\qquad GCD(325,2100,2125)=9\space$$ so that triple is not primitive.