Constructing magic squares: an integer constraint satisfaction problem and a fast approach

Abstract Magic squares are a fascinating mathematical challenge that has intrigued mathematicians for centuries. Given a positive (and possibly large) integer $$ n $$ <mml:math xmlns:mml="http://www.w3.org/1998/Math/MathML"> <mml:mi>n</mml:mi> </mml:math> , one of the main challenges that still remains is to find, within a reasonable computational time, a magic square of order $$ n $$ <mml:math xmlns:mml="http://www.w3.org/1998/Math/MathML"> <mml:mi>n</mml:mi> </mml:math> , that is, a square matrix of order $$ n $$ <mml:math xmlns:mml="http://www.w3.org/1998/Math/MathML"> <mml:mi>n</mml:mi> </mml:math> with unique integers from $$ a_{\min } $$ <mml:math xmlns:mml="http://www.w3.org/1998/Math/MathML"> <mml:msub> <mml:mi>a</mml:mi> <mml:mo>min</mml:mo> </mml:msub> </mml:math> to $$ a_{\max } $$ <mml:math xmlns:mml="http://www.w3.org/1998/Math/MathML"> <mml:msub> <mml:mi>a</mml:mi> <mml:mo>max</mml:mo> </mml:msub> </mml:math> , such that the sum of each row, column, and diagonal equals a constant $$ \mathcal {C}(A) $$ <mml:math xmlns:mml="http://www.w3.org/1998/Math/MathML"> <mml:mrow> <mml:mi>C</mml:mi> <mml:mo>(</mml:mo> <mml:mi>A</mml:mi> <mml:mo>)</mml:mo> </mml:mrow> </mml:math> . In this work, we first present an integer constraint satisfaction problem for constructing a magic square of order $$ n $$ <mml:math xmlns:mml="http://www.w3.org/1998/Math/MathML"> <mml:mi>n</mml:mi> </mml:math> . Nonetheless, the solution time of this problem grows exponentially as the order increases. To overcome this limitation, we also propose a fast approach that constructs magic squares depending on whether $$ n $$ <mml:math xmlns:mml="http://www.w3.org/1998/Math/MathML"> <mml:mi>n</mml:mi> </mml:math> 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 70 000 in less than 140 seconds, demonstrating its efficiency and scalability.

Authors

Institutions

Publication Details

Journal
Computational and Applied Mathematics
Published
2026-03-31
DOI
https://doi.org/10.1007/s40314-026-03731-3
Primary Topic
Graph Labeling and Dimension Problems
Type
article
Field-Weighted Citation Impact
0.00

Funders

Controls
|||
ALL TIME
JAN
FEB
MAR
APR
MAY
JUN
JUL
AUG
SEP
OCT
article

Constructing magic squares: an integer constraint satisfaction problem and a fast approach

Luiz-Rafael Santos, João Vitor Pamplona, Maria Eduarda Pinheiro
Computational and Applied Mathematics
Graph Labeling and Dimension Problems
article

Constructing magic squares: an integer constraint satisfaction problem and a fast approach

Luiz-Rafael Santos, João Vitor Pamplona, Maria Eduarda Pinheiro
article en

Abstract

Abstract Magic squares are a fascinating mathematical challenge that has intrigued mathematicians for centuries. Given a positive (and possibly large) integer $$ n $$ <mml:math xmlns:mml="http://www.w3.org/1998/Math/MathML"> <mml:mi>n</mml:mi> </mml:math> , one of the main challenges that still remains is to find, within a reasonable computational time, a magic square of order $$ n $$ <mml:math xmlns:mml="http://www.w3.org/1998/Math/MathML"> <mml:mi>n</mml:mi> </mml:math> , that is, a square matrix of order $$ n $$ <mml:math xmlns:mml="http://www.w3.org/1998/Math/MathML"> <mml:mi>n</mml:mi> </mml:math> with unique integers from $$ a_{\min } $$ <mml:math xmlns:mml="http://www.w3.org/1998/Math/MathML"> <mml:msub> <mml:mi>a</mml:mi> <mml:mo>min</mml:mo> </mml:msub> </mml:math> to $$ a_{\max } $$ <mml:math xmlns:mml="http://www.w3.org/1998/Math/MathML"> <mml:msub> <mml:mi>a</mml:mi> <mml:mo>max</mml:mo> </mml:msub> </mml:math> , such that the sum of each row, column, and diagonal equals a constant $$ \mathcal {C}(A) $$ <mml:math xmlns:mml="http://www.w3.org/1998/Math/MathML"> <mml:mrow> <mml:mi>C</mml:mi> <mml:mo>(</mml:mo> <mml:mi>A</mml:mi> <mml:mo>)</mml:mo> </mml:mrow> </mml:math> . In this work, we first present an integer constraint satisfaction problem for constructing a magic square of order $$ n $$ <mml:math xmlns:mml="http://www.w3.org/1998/Math/MathML"> <mml:mi>n</mml:mi> </mml:math> . Nonetheless, the solution time of this problem grows exponentially as the order increases. To overcome this limitation, we also propose a fast approach that constructs magic squares depending on whether $$ n $$ <mml:math xmlns:mml="http://www.w3.org/1998/Math/MathML"> <mml:mi>n</mml:mi> </mml:math> 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 70 000 in less than 140 seconds, demonstrating its efficiency and scalability.

Computational and Applied MathematicsVol. 45(8)
Universidade Federal de Santa Catarina (BR), Universidade Regional de Blumenau (BR)
Deutsche Forschungsgemeinschaft, Coordenação de Aperfeiçoamento de Pessoal de Nível Superior, Bundesministerium für Bildung und Forschung, Conselho Nacional de Desenvolvimento Científico e Tecnológico, Fundação de Amparo à Pesquisa e Inovação do Estado de Santa Catarina, Universidade Federal de Santa Catarina
Openalex Percentile: Top 35%
Graph Labeling and Dimension Problems
AI Navigator

Ask Laika to Summarize, Analyze, and Connect papers live on the map.

Summarize Papers & Methodologies

Extract key findings, datasets, and comparative methods across publications.

Benchmark Rankings & Visual Analytics

Rank top research institutions, authors, funders, topics, and journals by Field-Weighted Citation Impact (FWCI) and paper volume with instant charts.

Connect Distant Disciplines

Bridge topological clusters on the map to find hidden collaborative intersections.