Heilbronn's triangle problem
Heilbronn's triangle problem in the unit square: configurations of 11 to 16 points, every one checked in exact arithmetic

| 1 | #!/usr/bin/env python3 |
| 2 | """The twelve points of Comellas and Yebra (2002), page 7, from their closed form. |
| 3 | |
| 4 | x = 1 - (A**(2/3) + 6) / (6 * A**(1/3)), A = 27 + 3*sqrt(57), |
| 5 | y = 2*x**2 - 3*x + 1/2. |
| 6 | |
| 7 | The paper states H12 >= x/4 + x*y/2 - x**2/2 = 0.032599, the smallest positive |
| 8 | root of 64*t**3 + 80*t**2 + 28*t - 1 = 0 (0.0325988586... to ten decimals). |
| 9 | |
| 10 | Prints the twelve points `x y` with PRINT_DIGITS significant digits each. |
| 11 | """ |
| 12 | |
| 13 | import sys |
| 14 | from decimal import Decimal, getcontext |
| 15 | |
| 16 | getcontext().prec = 60 |
| 17 | PRINT_DIGITS = 45 |
| 18 | |
| 19 | |
| 20 | def cbrt(v): |
| 21 | """Cube root of a nonnegative Decimal, Newton's method.""" |
| 22 | if v == 0: |
| 23 | return Decimal(0) |
| 24 | guess = Decimal(repr(round(float(v) ** (1 / 3), 12))) |
| 25 | three = Decimal(3) |
| 26 | for _ in range(200): |
| 27 | nxt = (2 * guess + v / (guess * guess)) / three |
| 28 | if nxt == guess: |
| 29 | return guess |
| 30 | guess = nxt |
| 31 | return guess |
| 32 | |
| 33 | |
| 34 | def main(): |
| 35 | sqrt57 = Decimal(57).sqrt() |
| 36 | a = 27 + 3 * sqrt57 |
| 37 | a13 = cbrt(a) |
| 38 | a23 = a13 * a13 |
| 39 | x = 1 - (a23 + 6) / (6 * a13) |
| 40 | y = 2 * x * x - 3 * x + Decimal(1) / 2 |
| 41 | |
| 42 | # The paper's bound, and the cubic it should equal; assert both. |
| 43 | h = x / 4 + x * y / 2 - x * x / 2 |
| 44 | t = Decimal(1) |
| 45 | for _ in range(200): # Newton on 64 t^3 + 80 t^2 + 28 t - 1 |
| 46 | f = 64 * t**3 + 80 * t**2 + 28 * t - 1 |
| 47 | fp = 192 * t**2 + 160 * t + 28 |
| 48 | nxt = t - f / fp |
| 49 | if nxt == t: |
| 50 | break |
| 51 | t = nxt |
| 52 | assert abs(h - t) < Decimal("1e-50"), f"closed form mismatch: {h} vs {t}" |
| 53 | |
| 54 | points = [ |
| 55 | (x, Decimal(0)), (1 - x, Decimal(0)), (Decimal(0), x), (Decimal(1), x), |
| 56 | (Decimal(1) / 2, y), (y, Decimal(1) / 2), (1 - y, Decimal(1) / 2), |
| 57 | (Decimal(1) / 2, 1 - y), (Decimal(0), 1 - x), (Decimal(1), 1 - x), |
| 58 | (x, Decimal(1)), (1 - x, Decimal(1)), |
| 59 | ] |
| 60 | for px, py in points: |
| 61 | print(f"{px:.{PRINT_DIGITS}f} {py:.{PRINT_DIGITS}f}") |
| 62 | print(f"# x = {x}", file=sys.stderr) |
| 63 | print(f"# y = {y}", file=sys.stderr) |
| 64 | print(f"# x/4 + xy/2 - x^2/2 = {h}", file=sys.stderr) |
| 65 | |
| 66 | |
| 67 | if __name__ == "__main__": |
| 68 | main() |