### Random Post

### Recent Posts

### Recent Comments

### Archives

### Categories

- article (11)
- enigma (1,367)
- misc (4)
- project euler (2)
- puzzle (90)
- puzzle# (48)
- site news (58)
- tantalizer (94)
- teaser (7)

### Site Stats

- 233,129 hits

Programming Enigma Puzzles

26 May 2013

Posted by on **From New Scientist #2588, 27th January 2007**

I challenged Harry and Tom to draw this diagram, with the angles ABD, BEC and CED each 90° and each side of the three triangles an integral number (less than 40) of centimetres long.

They each managed to do this.

In Harry’s version (AB+BC) was the same length as (AD+DC); in Tom’s version (AB+BE) was the same length as (AD+DE); CE was the same length in both versions.

What was the length of the perimeter of the quadrilateral ABCD in (a) Harry’s version, (b) Tom’s version?

[enigma1427]

%d bloggers like this:

This Python program examines all possible combinations of Pythagorean triangles with hypotenuse less than 40 to find the answer. It runs in 51ms.

Solution:(a) 90 cm; (b) 76 cm.In my machine which has 2.6 Ghz processor, the execution time is 16ms.

Triangles BEC and DEC are the same for Tom and Harry. Only triangle ABD is different between them. To get Tom’s solution I had to comment out Harry’s constraint and output line – vice versa to get Harry’s solutions, for which the code is relevant.

Only Tom’s perimeter of 76 cm and Harry’s perimeter of 90 cm have the same value of EC (or CE)

Using Google’s Python interface to their Google Optimization Tools (or Jim’s MiniZinc wrapper), it is straightforward to produce a full constraint programming solution to this puzzle.

A blast from the past!

Here’s a MiniZinc model that generates possibilities for Harry’s and Tom’s triangles as output. It runs in 67ms (using the [[

`mzn-gecode -a`

]] solver).In fact there is only one possible triangle for Tom. There are two possible triangles for Harry, but only one of them has the same CE length as Tom’s triangle.

The following Python program executes the above MiniZinc code and analyses the output to find matching possibilities for Tom and Harry. It uses the

minizinc.pywrapper library (which, of course, I hadn’t written in 2013 when this puzzle was posted to the site). The overall execution time is 132ms.We can remove line 21 from the MiniZinc model to generate all possible diagrams (there are 45, the same number that my original program considers). The Python program only selects those that are applicable to Tom and Harry anyway, and the overall run time is the same.