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 | """Evaluate Comellas--Yebra (2002), page 7, using only the standard library.""" |
| 3 | import argparse |
| 4 | from decimal import Decimal, localcontext |
| 5 | from fractions import Fraction |
| 6 | |
| 7 | |
| 8 | def configuration(digits): |
| 9 | with localcontext() as ctx: |
| 10 | ctx.prec = digits + 40 |
| 11 | a = Decimal(27) + 3 * Decimal(57).sqrt() |
| 12 | u = Decimal(4) |
| 13 | for _ in range(100): |
| 14 | next_u = (2 * u + a / (u * u)) / 3 |
| 15 | if abs(next_u - u) < Decimal(10) ** (-(digits + 30)): |
| 16 | u = next_u |
| 17 | break |
| 18 | u = next_u |
| 19 | else: |
| 20 | raise ArithmeticError("cube root did not converge") |
| 21 | x = 1 - (u * u + 6) / (6 * u) |
| 22 | y = 2 * x * x - 3 * x + Decimal('0.5') |
| 23 | quantum = Decimal(1).scaleb(-digits) |
| 24 | # Round only these two parameters. Exact rational complements preserve |
| 25 | # the construction's symmetries in the submitted finite decimals. |
| 26 | x, y = (Fraction(v.quantize(quantum)) for v in (x, y)) |
| 27 | half = Fraction(1, 2) |
| 28 | return [(x, 0), (1-x, 0), (0, x), (1, x), |
| 29 | (half, y), (y, half), (1-y, half), (half, 1-y), |
| 30 | (0, 1-x), (1, 1-x), (x, 1), (1-x, 1)] |
| 31 | |
| 32 | |
| 33 | def fixed(value, digits): |
| 34 | scaled = Fraction(value) * 10 ** digits |
| 35 | if scaled.denominator != 1: |
| 36 | raise ArithmeticError("coordinate is not representable at this precision") |
| 37 | whole, tail = divmod(scaled.numerator, 10 ** digits) |
| 38 | return f'{whole}.{tail:0{digits}d}' |
| 39 | |
| 40 | |
| 41 | def main(): |
| 42 | parser = argparse.ArgumentParser(description=__doc__) |
| 43 | parser.add_argument('--digits', type=int, default=80) |
| 44 | args = parser.parse_args() |
| 45 | if not 6 <= args.digits <= 200: |
| 46 | parser.error('--digits must be between 6 and 200') |
| 47 | for x, y in configuration(args.digits): |
| 48 | print(f'{fixed(x, args.digits)} {fixed(y, args.digits)}') |
| 49 | |
| 50 | |
| 51 | if __name__ == '__main__': |
| 52 | main() |