Friday, January 11, 2019

Magic Square 2019

Magic Square 2019

Lets start with a magic square. In  a magic square the sum of the rows and cells are all the same
(magic) number and every number appears only once.

Dürer Magic

In 1514 Albrecht Dürer created a famous Magic Square contained in his work Melencolia, which also contained the numbers 15 and 14 next to each other i.e. the year of its creation. As you can check for yourself the sum of the numbers in each row and columns is the same number, called magic constant, in this case 34.

Dürers Magic Square (source Wikipedia)
If you are interested in the details of Magic Squares have a look at http://mathworld.wolfram.com/DuerersMagicSquare.html

Happy New Year Square

For the New Year i wanted to create a magic square for 2019 like Dürer did with 1514. Only then i realized, if one adds 5 to every cell of Dürer Square we already have (a kind of) magic square for 2019:


218718
10151613
14111217
920196

Ok, this would be too boring even for this blog.

So how can we get a real Magic Square 2019?

First of all we need a bigger square as 20 and 19 should appear in it. Let's use a 6x6 square so we can put the year in the middle of the last row. Then we'll have to add the additional constraints (all numbers must be different, the sums of rows and columns must be the same magic constant) and let the constraint solver do its magic.

An implementation using Google's CP-SAT Solver (Google's CP SAT Solver) can be found in this github gist Fun With CP Magic Square 2019.

If you are using Google's Colaboratoy (http://colab.research.google.com/) you should be able to open and run the notebook with "open in colab".

Alternatively you can use your local python Jupyter environment. It should be easy to port it to your favourite constraint solver.

Happy New Year &
Have fun with Constraint Programming!

PS:
Hope you had as much fun as:



No comments:

Post a Comment

Solving CSP problems in the browser with Sentient

It is surprising difficult to find a CSP Solver for solving CSP Problems directly in Javascript. One such systems is Sentient https://sent...