# A Function to Generate Pythagorean Triples

For a given right triangle $$ABC$$ with hypotenuse $$C$$, if you know that value of one of the legs, such as $$B$$, you can calculate possible values for the other two sides by using the equation $$"\dfrac{B^2}4\pm1"$$. For example, if you are given the value of $$4$$, solving would proceed as follows: $$4^2 = 16; \dfrac{16}4 = 4$$; and $$4\pm1 = 3,5$$ giving the other two sides lengths. This becomes the well-know $$3,4,5$$ Pythagorean triple. This equation will work for any value of A, and will give whole number outputs for any even number. For odd numbers, there is the slightly different equation "$$\dfrac{(B^2)\pm1}{2}$$". I am currently unaware of how to derive these equations, and knowing how would be most helpful.

Edit: The odd equation will work for any given number due to the following: (note that a very similar evaluation will work for the even version of the equation)

$${A^2}+{B^2}={C^2}$$, $$A=\dfrac{(B^2)-1}{2}$$, $$C=\dfrac{(B^2)+1}{2}$$. These are all given as they are part of the function.

$${(\dfrac{{B^2}-1}{2})^2}+{B^2}={(\dfrac{{B^2}+1}{2})^2}$$ by substituting for $$A$$ and $$C$$.

$${\dfrac{{B^4}-2{B^2}+1}4}+{B^2}={\dfrac{{B^4}+2{B^2}+1}4}$$ by squaring $$A$$ and $$C$$.

$${B^4}-2{B^2}+1+4{B^2}={B^4}+2{B^2}+1$$ by multiplying everything by $$4$$.

$$-2{B^2}+4{B^2}=2{B^2}$$ with some cancelling.

$$1=1$$ or $$0=0$$ through more cancelling.

Therefore, any value of $$B$$ will make the equation true. The reason that it works best with odd whole numbers is because "odd"$$*$$"odd"$$=$$"odd", "odd"$$\pm1=$$"even", and only even numbers leave whole values when halved.

Update: One of the comments asked if this equation could be modified for other values of $$C-A$$. After some thought, I have found a family of equations capable of producing triples with any value of $$C-A$$.

Let's assume that $$C-A=2x$$. There is a number $$\theta$$ whose value is halfway between $$C$$ and $$A$$, so therefore $$A=\theta -x$$, and $$C=\theta +x$$ Substituting these values into $${A^2}+{B^2}={C^2}$$ gives us the following:

$${(\theta-x)^2}+{B^2}={(\theta+x)^2}$$

$${B^2}+{\theta^2}-2\theta x+{x^2}={\theta^2}+2\theta x+{x^2}$$

$${B^2}=4\theta x$$

$$\theta ={\dfrac{B^2}{4x}}$$

Because $$B=\theta -x$$ and $$C=\theta +x$$:

$$B={\dfrac{A^2}{4x}}-x$$

$$C={\dfrac{A^2}{4x}}+x$$

If $$C-A=2$$, then $$x=1$$, and the equation simplifies to "$$\dfrac{B^2}4\pm1$$". Other values of x will work as well. For $$C-A=3$$, $$x=1.5$$, and the new equation is "$$\dfrac{B^2}6\pm1.5$$" This equation produces integers when $$B$$ is an odd multiple of $$3$$.

A similar process to the one above can be worked out for any other value of $$x$$.

• This works for differences of $2$ between the $B,C$ legs, or differences of $1$ between them for the "odd numbers" case you list. Have you considered other differences between these numbers? Commented Sep 1, 2021 at 13:48
• I see the limitations of the two equations, and am trying to find more than can work for greater differences. Commented Sep 1, 2021 at 13:50
• The wikipedia article en.wikipedia.org/wiki/Pythagorean_triple may be a good place to start on this, and I think I see some ways that the formulas you already have can be derived from what is there. Commented Sep 1, 2021 at 13:52
• You can use \pm in mathjax to make a single symbol that does the plus and minus you want. Commented Sep 1, 2021 at 15:25
• I slept on your question and edited my answer in a way that I hope is better at "answering" your question. Commented Sep 2, 2021 at 12:27

Your formula does generate all triples where $$\space C-A=2.\space$$ The only caution is that you must restrict input to $$4n \space$$ to ensure that only primitives are generated. To understand the validity of your formula we begin with a variation of Euclid's formula

\begin{align*} &A=\space\space (2n-1+k)^2-k^1\\ &B= 2(2m-1+k)k\\ &C=\space\space (2n-1+k)^2+k^1\\ \end{align*} $$\mathbf{\downarrow}$$

\begin{align*} A=(2n-1)^2+ & 2(2n-1)k \\ B= \qquad\quad\quad\space & 2(2n-1)k+ 2k^2\\ C=(2n-1)^2+ & 2(2n-1)k+ 2k^2\\ \end{align*}

that generates only and all triples where $$GCD(A,B,C)=(2x-1)^2, x\in\mathbb{N}.\quad$$
Since $$1$$ is an odd square, this subset includes all primitive Pythagorean triples.

$$\begin{array}{c|c|c|c|c|} n & k=1 & k=2 & k=3 & k=4 \\ \hline Set_1 & 3,4,5 & 5,12,13& 7,24,25& 9,40,41 \\ \hline Set_2 & 15,8,17 & 21,20,29 &27,36,45 &33,56,65 \\ \hline Set_3 & 35,12,37 & 45,28,53 &55,48,73 &65,72,97 \\ \hline Set_{4} &63,16,65 &77,36,85 &91,60,109 &105,88,137 \\ \hline Set_{5} &99,20,101 &117,44,125 &135,72,153 &153,104,185\\ \hline Set_{6} &143,24,145 &165,52,173 &187,84,205 &209,120,241\\ \hline \end{array}$$

We can see that column $$1$$ contains only "your" triples where $$\space C-A=2\space$$ and, if we let $$\space k=1,\space$$ the formula reduces to $$\quad \space A=4n^2-1\qquad B=4n\qquad C=4n^2+1.$$

To find how $$\space \dfrac{B^2}{4}+1=C,\space$$ we equate your formula's plus-side to the $$C$$-function.

Working backwards (Note:$$\space B=4n\space$$ no matter what formula is used) $$\frac{B^2}{4}+1=\left(\frac{(4n)^2}{4}\right)+1 =\frac{16n^2}{4}+1 =4n^2+1 =C$$

The same logic applies to the minus-side of your formula and, if we work the other direction from $$\space 4n^2\mp1,\space$$ we can see how your formula is correct, albeit limited in the subset of triples it generates.

$$\textbf{Update:}$$ Unfortunately, your formula cannot be generalized for $$\space C-A > 2\space$$ because $$\space C-A=2k^2 \in\big\{2,8,18,32,50\cdots\big\}$$ but the other two differences are asymmetric: \begin{align*} &C-B=(2n-1)^2\\ &B-A=P=p_1p_2p_3\cdots\space\text{where} \space P\equiv p_n\equiv \pm 1 \pmod 8 \end{align*}

$$\text{For example, with }\quad (57,176,185): \\ C-A=185-57=2\times8^2 \\ C-B=185-176=3^2 \\ B-A=176-57=7\times17$$

• @PiGuy314 Thank you for spotting my error. It is not a primitive triple, nor is any where A and C are even, but it is, indeed, a valid triple. Thanks again. Are there any other questions regarding the derivation of your formula? Commented Sep 2, 2021 at 19:16
• None at the moment. I really appreciate the effort you went through. Commented Sep 3, 2021 at 20:50