Mathematics Stack Exchange is a question and answer site for people studying math at any level and professionals in related fields. It's 100% free, no registration required.

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 trouble shooting my code I wrote to generate all Heronian Triangles (triangle with integer sides and integer area). I'm using the following algorithm

$$a=n(m^{2}+k^{2})$$ $$b=m(n^{2}+k^{2})$$ $$c=(m+n)(mn-k^{2})$$ $$\text{Semiperimeter}=mn(m+n)$$ $$\text{Area}=mnk(m+n)(mn-k^{2})$$

for integers $m$, $n$ and $k$ subject to the contraints:

$$\gcd{(m,n,k)}=1$$ $$mn > k^2 \ge m^2n/(2m+n)$$ $$m \ge n \ge 1$$

which I found here

The odd part is that this algorithm doesn't seem to ever generate 7, 24, 25 which is a Hero Triangle (a right triangle, in fact) with integer area 84.

I had originally assumed it was a breakdown in my code, but then I realized I can't seem to find any values of $m$, $n$, or $k$ within the constraints or even ignoring the constraints that generate this triangle.

I don't know which is which between which of the 7, 24, or 25 equal the $a$, $b$, or $c$, but I've tried manually solving for $m$ and $n$ using wolframalpha. Since $a$ and $b$ are symmetric (since $m$ and $n$ are symmetric when ignoring constraints), I really only have 3 sets of linear equations to check:

$$7=n(m^{2}+k^{2})$$ $$24=m(n^{2}+k^{2})$$ $$25=(m+n)(mn-k^{2})$$

Wolframalpha - Linear equations 1

$$24=n(m^{2}+k^{2})$$ $$25=m(n^{2}+k^{2})$$ $$7=(m+n)(mn-k^{2})$$

Wolframalpha - Linear equations 2

$$25=n(m^{2}+k^{2})$$ $$7=m(n^{2}+k^{2})$$ $$24=(m+n)(mn-k^{2})$$

Wolframalpha - Linear equations 3

None of which have integer solutions.

Is my understanding of Hero Triangles wrong? Is the algorithm wrong? Is my implementation wrong?

share|cite|improve this question
up vote 3 down vote accepted

This algorithm doesn't generate primitive triangles ($\text{gcd}(a,b,c)=1$), and in fact does NOT generate 7, 24, 25 (as you showed). It instead generates 14, 48, 50 when $m=7$, $n=1$ ,$k=1$.

Looks like when they say "All Heronian triangles can be generated as multiples of" this formula, they aren't just counting integer multiples (like 2*14, 2*48, 2*50), they are counting this like like .5*14, .5*48, .5*50, which is the triangle you were looking for, 7, 24, 25. To get to the primitive triangle in each generated case you can divide by the greatest common divisor of the 3 sides.

share|cite|improve this answer

Maybe these formulas, published by H Schubert in 1905, are better: $$a=mn(p^2+q^2);\quad b=pq(m^2+n^2);\quad c=(mq+np)(mp-nq)$$ with $\gcd(m,n)=1$. Then $m=n=1$, $p=4$, $q=3$ gives $a=25$, $b=24$, $c=7$.

share|cite|improve this answer

Schubert's paper can be found online here:

Note that $\gcd(p,q=1)$ also. This approach has the same problem as Brahmagupta's parameterization: not all primitive heron triangles are directly generated. In particular the 13,14,15 triangle is generated as the 26,28,30 triangle with parameters m=2, n=1, p=3, q=2.

Sascha Kurz discusses the subject here:

He gives an O(N^(2+e)) algorithm or two for generating triangles with sides smaller than N.

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.