Mathematics Stack Exchange is a question and answer site for people studying math at any level and professionals in related fields. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I'm looking for formulas or methods to find pythagorean triples. I only know one formula for calculating a pythagorean triple and that is euclid's which is:

$$\begin{align} &a = m^2-n^2 \\ &b = 2mn\\ &c = m^2+n^2 \end{align}$$

With numerous parameters.

So are there other formulas/methods?

share|cite|improve this question
What is wrong with this one? It gets all the primitive ones if $m,n$ have no common factor and one is even. – Ross Millikan May 18 '13 at 14:26
Why don't you like these formulas? – Boris Novikov May 18 '13 at 14:26
@RossMillikan I was looking for one that could be easier to implement into a program. I am making a program that generates pythagorean triples. I have implemented this formula but I would like to know if there are more or if there are better more advance ones. EDIT: Also, this formula is a little cumbersome to implement, anyway. – Jeel Shah May 18 '13 at 14:28
@gekkostate: That's the most efficient formula for generating primitive triples.Are you looking for all the triples in order? – Inceptio May 18 '13 at 14:33… – lab bhattacharjee May 18 '13 at 14:33
up vote 2 down vote accepted

It doesn't seem cumbersome to me. Just loop on $m$ starting at $2$. Loop on $n$ starting at $1$ or $2$ depending on the parity of $m$ and going up to $m-1$. Check the GCD using Euclid's algorithm. If it is $1$ you have a primitive set, so calculate $a,b,c$. If you want all sets up to some $N$, multiply $a,b,c$ by all values up to $\lfloor \frac Nc \rfloor$. Stop the $m$ loop at $\sqrt N$

share|cite|improve this answer

The one I was taught in my lectures was:

Assume that $(x, y, z)$ is a Pythagorean triple in which $x$ is odd, so that $y$ is even and $z$ is odd. For similar reasons, we assume that $p$ and $q$ are coprime $(x,y,z,p,q \in \mathbb{Z})$. The theory of Pythagorean triples then tells us that there are nonzero integers $p$ and $q$ (with $p > q$)such that

$$x + iy = (p + iq)^2 \hspace{1.5cm} z = |p + iq|^2 = p^2 + q^2.$$

If $x$ is odd then one of $p$ and $q$ must be even and the other is odd.

In case it isn't obvious, the way it works is this: let's first check with the smallest three triples. Let's pick two small numbers satisfying the criteria for $p$ and $q$, i.e take $p = 2$ and $q = 1$ (one of them is odd, both are co-prime and $p > q$). First, to find our $z$ value, we simply do $p^2 + q^2 = 1^2 + 2^2 = 5$. Now, let's calculate our $x$ and $y$ values:

$$x + iy = (p + iq)^2 = (2 + i)(2 + i) = 4 + 4i - 1 = 3 + 4i.$$

From here, we see that $x = 3$ and $y = 4$, which we know to be true as $3^2 + 4^2 = 5^2$/

This might not give you ALL the Pythagorean triples but its another way of doing it, seeing as you only need to pick $p,q$ and the rest are worked out for you.

The coding I've ever done is Matlab so I can only really think how to put this in terms of Matlab (and even then it might be wrong), so I'm not sure if it will work for your program or whatever you're making, but here is another way anyway.

share|cite|improve this answer

Euclid's method is only one of many for producing Pythagorean triples. See the following article for some other methods.


share|cite|improve this answer

Let $x$ be side of the Pythagorean triangle then all integral triples are function of $x$: $x\geq3$. This generates all possible integral triples. Trick:Take divisors of same parity (as of $x$) of squared $x$ and less than $x$; the number of triples is equal to no of such divisors. If these are $j$ values; corresponding $k$ values are obtained as quotient (same parity as of $x$) by dividing $x^2$ by each $j$. Then $x=\sqrt{j\cdot k};y=\frac{k-j}{2}; z=\frac{k+j}{2};\,(x,y,z)$ constitute Pythagorean triple. No of such triples=No of $j$ or $k$ divisors of $x^2$= Half the No of pairable divisors of Squared $x$ having parity those of $x$. This is Mir's Generalized Theorem Defining All Pythagorean Integral Triples As Function of side of Pythagorean triangle (say $x$).

share|cite|improve this answer
There might be something to this, but it's pretty close to illegible at the moment. Perhaps you could demonstrate it using $x = 15$ as one of the side lengths? – pjs36 Mar 23 at 15:34

I can tell formula for any odd number but this formula always works.

take any odd number=(x) . Solve for $(a+a+1 = x^2)$.

$(a)$ and $(a+1)$ will be the two numbers that will satisfy pythagoran triplet with $x$.

$(x)$ should be a natural number.

$(a+1)$ will be hypotenuse, $(a)$ will be the longer side ( if you consider perpendicular is greater, then $(a)$ is the perpendicular) and $(x)$ will be the shortest side.

This is the new trick which is invented by me shreyas.

share|cite|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.