English

Constructing Magic Squares: an integer constraint satisfaction problem and a fast approach

Optimization and Control 2026-01-06 v4

Abstract

Magic squares are a fascinating mathematical challenge that has intrigued mathematicians for centuries. Given a positive (and possibly large) integer n n , one of the main challenges that still remains is to find, within a computational time, a magic square of order n n , that is, a square matrix of order n n with unique integers from amin a_{\min} to amax a_{\max} , such that the sum of each row, column, and diagonal equals a constant C(A) \mathcal{C}(A) . In this work, we first present an integer constraint satisfaction problem for constructing a magic square of order n n . Nonetheless, the solution time of this problem grows exponentially as the order increases. To overcome this limitation, we also propose a that constructs magic squares depending on whether n n is odd, singly even, or doubly even. Moreover, we provide a proof of the correctness of this novel approach. Our numerical results show that our method can construct magic squares of order up to \num{70000} in less than \num{140} seconds, demonstrating its efficiency and scalability.

Keywords

Cite

@article{arxiv.2504.20017,
  title  = {Constructing Magic Squares: an integer constraint satisfaction problem and a fast approach},
  author = {João Vitor Pamplona and Maria Eduarda Pinheiro and Luiz-Rafael Santos},
  journal= {arXiv preprint arXiv:2504.20017},
  year   = {2026}
}
R2 v1 2026-06-28T23:14:07.491Z