Skip to content

Instantly share code, notes, and snippets.

@nomeaning777
Created August 22, 2012 23:40
Show Gist options
  • Save nomeaning777/3430599 to your computer and use it in GitHub Desktop.
Save nomeaning777/3430599 to your computer and use it in GitHub Desktop.
(setq used (make-array 9 :initial-element t)
answer ()
permutation (make-array 9))
(defun number (l r)
(do ((i l (1+ i)) (ret 0)) ((= i r) ret)
(setq ret (+ (* ret 10) (aref permutation i)))))
(defun doIt ()
(do ((r 2 (1+ r))) ((= r 9))
(do ((l 1 (1+ l))) ((= l r))
(if (= (number r 9) (* (number 0 l) (number l r)))
(setq answer (cons (number r 9) answer))))))
(defun make-permutation (k)
(if (= k 9)
(doIt)
(dotimes (x 9)
(cond ((aref used x)
(setf (aref used x) nil)
(setf (aref permutation k) (1+ x))
(make-permutation (1+ k))
(setf (aref used x) t))))))
(make-permutation 0)
(setq answer (reduce #'+ (remove-duplicates answer)))
(print answer)
; => 45228
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment