MagicSquares
Finds magic squares (http://en.wikipedia.org/wiki/Magic_square) of an arbitrary order
Install / Use
/learn @cjlarose/MagicSquaresREADME
Magic Squares
Finds magic squares (Wikipedia) of an arbitrary order n.
It first generates a list of every possible permutation of n integers that sum to the magic constant, which represents all possible rows, columns, and diagonals. For any one of those permutations, it attempts to construct a matrix whose first row is that permutation and whose first column is a another permutation that begins with the same entry and contains no other duplicate entries. Then, the algorithm attempts to find sum permutations to fill in the remaining rows and columns.
On my laptop with a 2.7GHz Intel Core i7 processor, the program finds all magic squares of order n in the following runtimes:
<table> <tr><th>Order</th><th>Mean Runtime</th><th>Std. Dev.</th><th>Distinct Magic Squares</th></tr> <tr><td>1</td><td>.4ms</td><td>.9ms</td><td>1</td></tr> <tr><td>2</td><td>.4ms</td><td>.5ms</td><td>0</td></tr> <tr><td>3</td><td>6.0ms</td><td>1.7ms</td><td>1</td></tr> <tr><td>4</td><td>1.8seconds</td><td>.3seconds</td><td>880</td></tr> </table>You can generate statistics of the program on your own machine with the generateRuntimeStats() test method.
It's probably not a great idea to try to run this for n >= 5. It'll work, it just might take a few years. That's my disclaimer.
Note: This program uses the Fork/Join framework and therefore requires Java SE 7 to compile and run.
Usage:
javac MagicSquares.java
java MagicSquares 3
Related Skills
node-connect
347.9kDiagnose OpenClaw node connection and pairing failures for Android, iOS, and macOS companion apps
frontend-design
108.7kCreate distinctive, production-grade frontend interfaces with high design quality. Use this skill when the user asks to build web components, pages, or applications. Generates creative, polished code that avoids generic AI aesthetics.
openai-whisper-api
347.9kTranscribe audio via OpenAI Audio Transcriptions API (Whisper).
qqbot-media
347.9kQQBot 富媒体收发能力。使用 <qqmedia> 标签,系统根据文件扩展名自动识别类型(图片/语音/视频/文件)。
