GitXplorerGitXplorer
c

MagicSquares

public
5 stars
3 forks
0 issues

Commits

List of commits on branch master.
Unverified
3e22363311a1314494a57f673a11a2169c269446

Merge pull request #1 from gustavopinto/master

ccjlarose committed 10 years ago
Unverified
088447e39faca8f4c9678500ace70853ef25e2c5

Adding another constructor.

committed 10 years ago
Unverified
f2d273dfca99a13f219dc2d761e65d807f4fad2d

Updating ForkJoin usage for better performance and energy efficiency

committed 10 years ago
Unverified
3582eb6a4a8c7ac95311f245b2880679a91c920d

Moved stuff around

ccjlarose committed 11 years ago
Unverified
af848fe0be5979eb9fa41e0664e834e6d0a71fbe

Considering all diagonals, being more concerned about analysis than reducing the problem set

ccjlarose committed 12 years ago
Unverified
fdda67d7e37d7988a32b21506cdd48867c39aa2a

Removing half of all diagonals to test

ccjlarose committed 12 years ago

README

The README file for this repository.

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:

Order Mean Runtime Std. Dev. Distinct Magic Squares
1 .4ms .9ms 1
2 .4ms .5ms 0
3 6.0ms 1.7ms 1
4 1.8seconds .3seconds 880

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