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 |
def solve_puzzle(): original_perimeter = 0 polygon_perimeter = 0 polygon_area = 0 # Iterate through all possible dimensions of the rectangular card for width in range(1, 50): for height in range(1, 50): original_perimeter = 2 * (width + height) # Calculate the lengths of the sides of the resulting polygon side1 = width side2 = height side3 = ((width ** 2) + (height ** 2)) ** 0.5 # Calculate the perimeter of the resulting polygon polygon_perimeter = side1 + side2 + side3 # Check if the perimeters satisfy the given condition if polygon_perimeter == original_perimeter - 5/28: # Calculate the area of the polygon semi_perimeter = polygon_perimeter / 2 polygon_area = (semi_perimeter * (semi_perimeter - side1) * (semi_perimeter - side2) * (semi_perimeter - side3)) ** 0.5 return polygon_area return None # Call the function to solve the puzzle result = solve_puzzle() # Print the area of the polygon if a solution is found if result: print(f"The area of the polygon is: {result} square centimeters") else: print("No solution found.") |

Jim Randell already has used Pythagorean triples. Luckily I found a different approach (divisors).

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 |
# L1 = (-h/w)x + h # line L1 passes through 2 corners (w, 0) and (0, h) # L2 = (w/h)x + h/2 - w**2/(2h) # both lines are perpendicular to each other and cross at (w/2, h/2) # new triangle has two sides of length <w>, 2 sides of length <o> # and one side of length <x> for w in range(1, 49): # height is longer than width w2 = w * w # only check for divisors of w*w because of x2 formula for h in [x for x in range(w + 1, 50) if w2 % x == 0]: # consider triangle with sides x, w and (h - 2 * o) #x2 = (h - 2o)**2 + w2 #x2 = ((h2 - 2oh) / h)**2 + w2 # use that 2oh equals h2 - w2 (see below) #x2 = ((h2 - (h2 - w2)) / h)**2 + w2 x2 = (w2 / h)**2 + w2 # x2 has to be square if (x := x2**.5) % 1: continue # calculate side <o> which occurs twice # o equals L2(0) which is h/2 - w**2/(2h) o, r = divmod(h * h - w2, 2 * h) if r: continue # new perimeter is five twenty-eighths smaller than the old perimeter O = 2 * (h + w) N = 2 * (w + o) + x if 23 * O != 28 * N: continue # area is wo + (h-2o)w/2 + wo/2 A = w * (h + o) / 2 print("answer:", A, "cm^2") |

@Brian, when I use (without even calling divisors()):

1 2 3 |
from number_theory import divisors |

the program runs considerably slower . This doesn’t happen when I import divisors from enigma.

]]>The rectangle has sides of length a and b and its diagonal is of length c; the fold line, which crosses the diagonal at its mid-point and is perpendicular to it, is of length d. The relationships between the various lengths can be derived using similar triangles. Since the fold line has an integer length, we know that the diagonal must be rational which means that (a, b, c) a Pythagorean triple.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
# the height of the rectangle for a in range(1, 50): # the width of the rectangle for b in range(a, 50): # compute the length of the diagonal c = (a * a + b * b) ** (1 / 2) # the length of the fold line (an integer) d = a * c / b if round(d) == d: # the smallest side of the polygon (also an integer) s = (b * b - a * a) / (2 * b) if round(s) == s: # the perimeter condition (redundant) p1, p2 = 2 * (a + b), 2 * (a + s) + d if 23 * p1 == 28 * p2: area = a * b / 2 + a * (b * b - a * a) / (4 * b) print(f"Area = {area}cm^2 (rectangle: {a}cm x {b}cm).") |