# Sunday Times Teaser 2773 – King Lear III

### by Nick MacKinnon

#### Published: 15 November 2015 (link)

King Lear III had a square kingdom divided into sixteen equal-sized smaller square plots, numbered in the usual way. He decided to keep a realm for himself and share the rest into equal realms (larger than his) for his daughters, a “realm” being a collection of one or more connected plots. He chose a suitable realm for himself and one for his eldest daughter and he noticed that, in each case, multiplying together any plot numbers within the realm gave a perfect square. Then he found that there was only one way to divide the remainder of the kingdom into suitable realms.

List the plot numbers of the eldest daughter’s realm and of the king’s realm.

Hi Brian,
I couldn’t get your solution to run (no module named ‘number_theory’). Here is my simplistic solution:

By inspection the last 2 solutions are not contiguous; the second solution is the only unique division of the other 2 daughters with Lear keeping 16.

EDIT: I see your program has now changed – it runs perfectly.

Hi Trevor,

I have tidied up your program a bit – I hope you don’t mind. You can format your Python code yourself by adding tags – see the ‘About’ item in the menu above.

For future reference, many of my solutions use my number theory library which is available here

I appreciate the tidying up – I couldn’t find how to wrap lines.
I see it’s in PEP8.
I don’t understand the ‘adding tags’ story – I can’t find anything in About?
Unless you meant ‘tabs’ – in which case I’ve already set my indent to 2 spaces.
Also as a matter of interest it seems like your editor has line nos. whereas the IDLE editor doesn’t. What editor do you use?

Hi Trevor,

A ‘tag’ is just text you put inside square brackets (i.e. [ and ]) or inside angle brackets (i.e. < and >) to indicate that the following text is to be formatted in a specific way. If you add the text ‘pre’ inside angle brackets (without the quotes) before your Python code and then the text ‘/pre’ (again inside angle brackets without the quotes) after your code, it will then be formatted as the Python code above is. This is explained in the ‘About’ menu item above. This will also display line numbers for your code automatically (my Python IDE is Wing, which doesn’t add line numbers).

I do think that 2 space indents are good as this helps to save horizontal space when code is being displayed. And it is best to add brackets around lines when you want to put them on more than one lines (as I have done above). I also like to use PEP8 guidelines for code display as this helps people understand what it is doing.

Thanks Brian – got it.

7. I thought this was quite a good puzzle, involving several steps to arrive at the solution.

This Python program uses several useful routines from the enigma.py library. It runs in 135ms.

Solution: The Eldest daughter gets plots 1, 2, 5, 9 and 10. The King keeps plot 16.

The division of the kingdom is shown below:

There are three daughters, who each receive a realm of 5 plots. The King keeps a realm of 1 plot for himself.

The Eldest daughter’s realm is shaded red, the other two daughter’s realms are shaded blue. The King keeps the plot shaded black.I thought it was quite an interesting puzzle, although I did alter the wording slightly so that it made more sense.

The equivalent problem considering the sum of the squares in the realm (rather than the product) has many solutions. (You can just change the call to multiply() to use sum() instead).