Skip to content

Instantly share code, notes, and snippets.

@kencoba
Created March 9, 2011 09:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kencoba/861934 to your computer and use it in GitHub Desktop.
Save kencoba/861934 to your computer and use it in GitHub Desktop.
3 * 3 magic square
(use '[clojure.contrib.combinatorics :only (permutations)])
(for [pattern (permutations [1 2 3 4 5 6 7 8 9])
:when
(let [a1 (nth pattern 0)
a2 (nth pattern 1)
a3 (nth pattern 2)
b1 (nth pattern 3)
b2 (nth pattern 4)
b3 (nth pattern 5)
c1 (nth pattern 6)
c2 (nth pattern 7)
c3 (nth pattern 8)]
(= (+ a1 a2 a3)
(+ b1 b2 b3)
(+ c1 c2 c3)
(+ a1 b1 c1)
(+ a2 b2 c2)
(+ a3 b3 c3)
(+ a1 b2 c3)
(+ a3 b2 c1)))]
pattern)
(comment
((2 7 6 9 5 1 4 3 8)
(2 9 4 7 5 3 6 1 8)
(4 3 8 9 5 1 2 7 6)
(4 9 2 3 5 7 8 1 6)
(6 1 8 7 5 3 2 9 4)
(6 7 2 1 5 9 8 3 4)
(8 1 6 3 5 7 4 9 2)
(8 3 4 1 5 9 6 7 2)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment