# Enigmatic Code

Programming Enigma Puzzles

## Enigma 351: Four flies

From New Scientist #1500, 20th March 1986 [link]

Four unfriendly flies are sitting watching the cricket. Each is on the boundary of the ground, which is perfectly circular. Their names, in order round the edge, are At, Bet, Cot and Dut.

The length of each of the straight lines At-Bet, Bet-Cot, Cot-Dut and Dut-At is an exact whole number of flymins (this is, the flies’ unit of distance). Two of those lines actually have the same length. Furthermore, the total of these four lengths is precisely the same number as the area (in square flymins) of the quadrilateral At-Bet-Cot-Dut.

If I tell you that that area does not include the pitch (which is at the centre of the ground), can you tell me the four lengths?

[enigma351]

### 2 responses to “Enigma 351: Four flies”

1. Jim Randell 1 July 2016 at 7:43 am

As the flies are on the perimeter of a circle, the quadrilateral ABCD is cyclic. And there are several properties of cyclic quadrilaterals with sides a, b, c, d that we can use to solve this problem.

Firstly the semiperimeter of the quadrilateral is:

$s = \frac{ 1 }{ 2 }(a + b + c + d)$

The area of the quadrilateral is:

$A = \sqrt{ (s - a)(s - b)(s - c)(s - d) }$

And the circumradius, R, (the radius of the circumcircle) is given by:

$4AR = \sqrt{ (ab + cd)(ac + bd)(ad + bc) }$

We know that A, B, C, D lie on a semicircle (as the quadrilateral ABCD does not contain the centre of the circumcircle). So as we move from A to B to C to D we are advancing around the semicircle with straight line distances a, b, c and the final return to A (with distance d) is the longest side of the quadrilateral. So two of a, b, c are the same.

Also d is the longest side, and d < a + b + c. It doesn’t matter what order the a, b, c sides occur in, so we will assume a < c and b = a or b = c.

This Python program considers increasing values for d, and finds corresponding a, b, c values that form a cyclic quadrilateral. It then computes the angles subtended at the centre on the circumcircle by AB, BC and CD, in order for A, B, C, D to lie within a semicircle of the circumcircle these angles must sum to less than 180° (or π radians).

The program runs in 53ms.

from itertools import count
from fractions import Fraction as F
from math import acos, pi, sqrt, degrees
from enigma import irange, printf

# the four sides of the quadrilateral are a, b, c, d
# with d < a + b + c, and two of a, b, c the same

def solve():
for d in count(3):
for a in irange(1, d - 2):
for c in irange(a + 1, d - 1):
for b in (a, c):

# perimeter
p = a + b + c + d

# area^2
(A2, r) = divmod((p - 2 * a) * (p - 2 * b) * (p - 2 * c) * (p - 2 * d), 16)
if r > 0: continue

# check area == perimeter
if A2 == p * p:

R2 = F((a * b + c * d) * (a * c + b * d) * (a * d + b * c), 16 * A2)

# accumulate angles at the centre for a, b, c
theta = sum(acos(1.0 - (z * z) / (2 * R2)) for z in (a, b, c))
if not(theta < pi): continue

yield (a, b, c, d, R2, theta)

# find and output the first solution
for (a, b, c, d, R2, theta) in solve():
printf("a={a} b={b} c={c} d={d}, R={R} (R^2={R2}), theta={theta} degrees", R=sqrt(R2), theta=degrees(theta))
break


Solution: The four lengths are: 5, 5, 6, 14 flymins.

The radius of the circumcircle is (5/6)√109 (≈ 8.700) flymins, and the flies are contained in a sector of the circumcircle spanning ≈107.13°.

There is another cyclic quadrilateral with integer sides, namely, 2, 5, 5, 8 flymins, but this encloses the centre of the circumcircle as shown below:

In this case the radius of the circumcircle is (5/8)√41 (≈ 4.002) flymins, but the flies span ≈183.6° of the circumcircle.

2. Brian Gladman 2 July 2016 at 12:04 pm
from itertools import count
from math import asin, pi

# the longest side of the quadrilateral
for c in count(3):
# the side that occurs twice
for a in range(1, c):
# the remaining side
for b in range(max(1, c - 2 * a + 1), c):

# the area is given by (b + c).sqrt{4.a^2 - (b - c)^2} / 4
t = 4 * a ** 2 - (b - c) ** 2
rt = int(t ** 0.5)
A, r = divmod(rt * (b + c), 4)

# ensure that the area is an integer and that it is
# equal to the perimiter
if not r and rt ** 2 == t and A == 2 * a + b + c:

# the diameter of the circumcircle is given by
# a.(b + c).sqrt(a^2 + b.c) / (2.A)
d = 0.5 * a * (b + c) * (a ** 2 + b * c) ** 0.5 / A

# ensure that the quadrileteral excludes the centre of
# the circumcircle
if 4 * asin(a / d) + 2 * asin(b / d) < pi:

t = ', '.join(str(x) for x in sorted((a, a, b, c)))
s, _, e = t.rpartition(', ')
print('The four sides are {} and {}.'.format(s, e))
exit(0)