# Enigmatic Code

Programming Enigma Puzzles

## Enigma 1170: Folderol

From New Scientist #2326, 19th January 2002 [link]

I have a rectangular piece of paper which I have folded twice and then unfolded again. The first fold was as shown:

In that figure the area of each of the three triangles is a perfect square number of cm², and each area is less than 100 [cm²].

The second fold was through X and parallel to the first. When unfolded the two creases divided the rectangle into three regions, namely two triangles and a pentagon. The area of each was again a perfect square number of cm².

What is the area of the rectangle?

[enigma1170]

### 2 responses to “Enigma 1170: Folderol”

1. Jim Randell 25 April 2016 at 8:20 am

I found the analysis a bit fiddly on this one. The maths isn’t that hard, but it is easy to make a mistake. I used SymPy to do some of the expression manipulation for me.

Suppose we are given the square numbers for triangles A and B, 0 < a < b < 10.

Now, if triangle A (area ) has width x and height y, then:

a² = (xy) / 2
x = (2a²) / y

Triangle B (area ) is (mathematically) similar to triangle A, and the sides are in ratio r = b / a. Its hypotenuse is the bottom edge of the paper, which has length x + ry. So:

(x + ry)² = (rx)² + (ry)²
x² + 2rxy + (ry)² = r²x² + (ry)²

Substituting for r and x and solving for y:

y² = (a / b)(b² − a²)

and it follows for triangle C (area ) that:

a² + b² + 2c² = rx(x + ry)

2c² = (a² + b²)² / (b² − a²)

For the lower triangle C, where the angle in the right hand corner is φ we have:

tan(φ) = (rx − y) / (x + ry) = a / b

and, in the second diagram, the triangle D has area:

d² = (1/2)(ry)² tan(φ)

d² = (b² − a²) / 2

and the region E has area:

e² = a² + b² + c² − d²

So, given a and b, we can work out c, d, e and check that they are all integers from 1 to 9.

This Python program looks at all the possibilities in 32ms.

```from itertools import combinations
from enigma import irange, sqrt, printf

# squares we are interested in
squares = dict((i * i, i) for i in irange(1, 9))

# choose squares for A = a^2 and B = b^2 (0 < a < b < 10)
for s in combinations(squares.keys(), 2):
(a2, b2) = sorted(s)

# compute c, d, e
p = b2 - a2
(d2, z) = divmod(p, 2)
if z != 0 or d2 not in squares: continue
d = squares[d2]
(c, z) = divmod(a2 + b2, 2 * d)
if z != 0: continue
c2 = c * c
if c2 not in squares: continue
e2 = a2 + b2 + c2 - d2
if e2 not in squares: continue
e = squares[e2]

# total area
T = a2 + b2 + 2 * c2

# compute x, y, r, w, h (not necessarily integers)
(a, b) = (squares[a2], squares[b2])
r = float(b) / float(a)
y = sqrt(float(p) / r)
x = float(2 * a2) / y
w = x + r * y
h = r * x
printf("a={a} b={b} c={c} d={d} e={e}, r={r} x={x} y={y}, T={T} w={w} h={h}")
```

Solution: The rectangle has area 90 cm².

Here are some diagrams, which should help explain things:

The computed values are:

a = 2
b = 6
c = 5
d = 4
e = 7

r = 3
x = √6 ≈ 2.449
y = (4/3)√6 ≈ 3.266

T = 90
w = 5√6 ≈ 12.247
h = 3√6 ≈ 7.348

2. Brian Gladman 26 April 2016 at 8:56 pm

My initial attempt at the maths got too complicated until I realised that the tangent formula for double angles provided a simple solution. A nice challenge.

```from itertools import combinations
from fractions import Fraction as RF
from math import pi, atan

# Let the width and height of the rectangle be w and h respectively
# and let t = tan[theta] where theta is the angle at the right hand
# vertex of the lower triangle. Let the areas of the upper left and
# right triangles be a^2 and b^2.   These are similar triangles and
# have hypotenuses of w.t and t so the ratio of their areas is t^2,
# which gives:
#
#    t = tan[theta] = a / b
#
# The upper right triangle hypotenuse has an angle of 2.theta with
# the upper side of the rectangle so:
#
#    h = w.sin[2.theta] = 2.w.t / (1 + t^2)
#    h.w = A = 2.w^2.t / (1 + t^2)
#
# where A is the area of the rectangle. The area of the upper right
# triangle (b^2) is given by:
#
#    b^2 = (1/2).w^2.sin[2.theta].cos[2.theta]
#        = (1/2).(w^2).{2.t / (1 + t^2)}.{(1 - t^2) / (1 + t^2)}
#          {2.t.w^2 / (1 + t^2)}{(1 - t^2) / (1 + t^2)}
# (1)    = (A/2).(1 - t^2) / (1 + t^2)
#
# The lower triangle has an area given by (1/2).w^2.t, which gives:
#
#    c^2 = (1/2).w^2.t = (w.h/4).(1 + t^2) = (A/4).(1 + t^2)
#
# The upper right triangle formed by the second fold has an area
# (d^2) given by:
#
#    d^2 = (1/2).{w / tan[2.theta]}^2.tan[theta]
#        = (A/4).(1 - t^2)^2 / (1 + t^2)
#
# We know that a and b are less than 10, so we can use these to
# set tan[theta]. We can then use (1) above to look for a value
# of A that makes the right hand side a perfect square.  We can
# then compute c^2,  d^2 and e^2 = A - c^2 - d^2 and check that
# they are all perfect squares.

# return the square root of a fraction if it is a perfect
# square, otherwise raise an exception

class Imperfect(Exception):
pass

def rf_sqrt(x):
if x.denominator == 1 and x.numerator >= 0:
t = int (x.numerator ** 0.5)
if t * t == x.numerator:
return t
raise Imperfect

# try possible combinations of a nand b
for a, b in combinations(range(1, 10), 2):
# form tan[theta]
t = RF(a, b)
# compute the area of the rectangle
A = 2 * b * b * (1 + t * t) / (1 - t * t)
try:
# now compute the two other squares
c = rf_sqrt(A * (1 + t * t) / 4)
d = rf_sqrt(A * (1 - t * t) ** 2 / (4 * (1 + t * t)))
except Imperfect:
# skipping if they are not both perfect
continue
# check the final perfect square
e2 = A - c * c - d * d
e = int(e2 ** 0.5 + 0.5)
if e * e == e2:
h = float(2 * t * A / (1 + t ** 2)) ** 0.5
f1 = 'Rectangle Area = {} cm^2 (width = {:.3f}, height = {:.3f}).'
print(f1.format(A, A / h, h))
f2 = 'a = {}, b = {}, c = {}, d = {}, e = {}, theta = {:.3f} degrees.'
print(f2.format(a, b, c, d, e, 180 * atan(float(t)) / pi))
```