π€ AI Summary
This paper addresses the efficient construction of binary magic squares (BMS)βi.e., {0,1}-matrices with equal row and column sums. It formally defines and characterizes the existence conditions for non-square BMS: an *m*Γ*n* BMS with target sum *s* exists if and only if βmax(*m*,*n*)/2β β€ *s* β€ βmin(*m*,*n*)/2β. We propose a constructive algorithm based on mathematical induction, achieving optimal theoretical time complexity *O*(*mn*). The algorithm is implemented with dual CPU/GPU backends supporting parallel batch generation, guaranteeing correctness of all outputs. Our contributions include: (i) the first necessary and sufficient existence theorem for rectangular BMS; (ii) the first deterministic, asymptotically optimal construction algorithm; and (iii) an industrial-grade, open-source Python toolkit enabling real-time generation of million-scale BMS instances.
π Abstract
We propose a simple algorithm for generating Binary Magic Squares (BMS), i.e., square binary matrices where the sum of all rows and all columns are equal. We show by induction that our algorithm always returns valid BMS with optimal theoretical complexity. We then extend our study to non-square Binary Magic Squares, formalize conditions on the sum of rows and columns for these BMS to exist, and show that a slight variant of our first algorithm can generate provably generate them. Finally, we publicly release two implementations of our algorithm as Python packages, including one that can generate several BMS in parallel using GPU acceleration.