1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
# suppose the triangle has sides a, b, c # where a < b, and c = b + (b - a) = 2b - a # # by Heron's formula the area of the triangle T is: # # (4T)^2 = (a + b + c)(b + c - a)(a + c - b)(a + b - c) # # and we need to find triangles such that T = a + b + c from itertools import count from enigma import irange, printf def solve(): for b in count(2): for a in irange(1, b - 1): c = 2 * b - a if not(c < a + b): continue # compute (4T)^2 both ways x1 = (a + b + c) * (b + c - a) * (a + c - b) * (a + b - c) x2 = (4 * (a + b + c)) ** 2 if x1 == x2: printf("a={a} b={b} c={c}") return solve() |

I was too lazy to get a factor subroutine out although I would have done so had it been more of a challenge.

I also went on to solve for the triangle \((a, b, c=2b-a)\) with an area that is an integer multiple (\(k\)) of the semi-perimeter (\(s\)), which leads to the quadratic \[(3b – 2a)(2a – b) = 48k^2\] and the solution \[b = (2/3)\{2a\pm\sqrt{a^2-(3k)^2}\}\]

This equation also shows that the Pythagorean triple \((3k, 4k, 5k)\) provides a solution for any \(k\) value, although not all solutions are necessarily Pythagorean.

For example, the triangle \((13,14,15)\) provides another solution when \(k=4\).

]]>
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
# suppose the triangle has sides a, b, c # where a < b, and c = b + (b - a) = 2b - a # # the semi-perimeter of the triangle s = (a + b + c)/2 = 3b/2 # # and by Heron's formula the area of the triangle T is: # # T^2 = s(s - a)(s - b)(s - c) # # but T = 2s, hence: # # s(s - a)(s - b)(s - c) = 4s^2 # (s - a)(s - b)(s - c) = 4s # # substituting s = 3b/2 and eliminating c: # # (3b/2 - a)(3b/2 - b)(3b/2 - c) = 6b # (3b - 2a)(2a - b) = 48 # # both factors are positive integers, say f and g, f < g # such that f.g = 48 # # 2a - b = f # 3b - 2a = g # # hence: # # b = (f + g)/2, a = (f + b)/2 from enigma import divisors, printf n = 48 for f in divisors(n): g = n // f if g < f: break (b, r) = divmod(f + g, 2) if r: continue (a, r) = divmod(f + b, 2) if r: continue c = 2 * b - a printf("a={a} b={b} c={c}") |

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
# let the two smallest sides be a and b with a < b. To make b the # average of the other two sides requires that c = 2 * b - a. Now # using Heron's formula for the area and equating the area and the # perimeter we can show that: # # (3 * b - 2 * a) * (2 * a - b) = 48 # # so the bracketed expressions on the left are factors of 48. With # these factors as f and g we obtain: # # a = (f + 3 * g) / 4 and b = (f + g) / 4 for f in range(1, 49): g, r = divmod(48, f) (a, s), b = divmod(f + 3 * g, 4), (f + g) // 2 if not (r or s) and a < b: print('The sides are {:d}, {:d} and {:d} metres.' .format(a, b, 2 * b - a)) |