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 http://en.wikipedia.org/wiki/Integer_triangle#Heronian_triangles

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?


Solution 1:

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.

Solution 2:

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$.