Skip to content

Instantly share code, notes, and snippets.

@kencoba
Created March 9, 2011 08:53
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/861902 to your computer and use it in GitHub Desktop.
Save kencoba/861902 to your computer and use it in GitHub Desktop.
Komachi calculation problem
(defn eval-expr [expr]
(loop [f expr]
(let [fst (nth f 0)
op1 (nth f 1)
scd (nth f 2)]
(if (= (count f) 3) (op1 fst scd)
(let [op2 (nth f 3)
thd (nth f 4)
rst (drop 5 f)]
(if (or (= op2 #'+) (= op2 #'-))
(recur (concat (list (op1 fst scd)) (drop 3 f)))
(recur (concat (list fst op1 (op2 scd thd)) rst))))))))
(defn make-exprlist []
(let [f [#'+ #'- #'* #'/]]
(for [f1 f f2 f f3 f f4 f f5 f f6 f f7 f f8 f]
(list 1 f1 2 f2 3 f3 4 f4 5 f5 6 f6 7 f7 8 f8 9))))
(defn komachi []
(for [flist (make-exprlist)
:when (= 100 (eval-expr flist))]
flist))
(comment
((1 :+ 2 :+ 3 :+ 4 :+ 5 :+ 6 :+ 7 :+ 8 :* 9)
(1 :+ 2 :+ 3 :- 4 :* 5 :+ 6 :* 7 :+ 8 :* 9)
(1 :+ 2 :- 3 :* 4 :+ 5 :* 6 :+ 7 :+ 8 :* 9)
(1 :+ 2 :- 3 :* 4 :- 5 :+ 6 :* 7 :+ 8 :* 9)
(1 :+ 2 :* 3 :+ 4 :* 5 :- 6 :+ 7 :+ 8 :* 9)
(1 :+ 2 :* 3 :* 4 :* 5 :/ 6 :+ 7 :+ 8 :* 9)
(1 :- 2 :+ 3 :* 4 :* 5 :+ 6 :* 7 :+ 8 :- 9)
(1 :- 2 :+ 3 :* 4 :* 5 :- 6 :+ 7 :* 8 :- 9)
(1 :- 2 :* 3 :+ 4 :* 5 :+ 6 :+ 7 :+ 8 :* 9)
(1 :- 2 :* 3 :- 4 :+ 5 :* 6 :+ 7 :+ 8 :* 9)
(1 :- 2 :* 3 :- 4 :- 5 :+ 6 :* 7 :+ 8 :* 9)
(1 :* 2 :* 3 :+ 4 :+ 5 :+ 6 :+ 7 :+ 8 :* 9)
(1 :* 2 :* 3 :- 4 :* 5 :+ 6 :* 7 :+ 8 :* 9)
(1 :* 2 :* 3 :* 4 :+ 5 :+ 6 :+ 7 :* 8 :+ 9)
(1 :* 2 :* 3 :* 4 :+ 5 :+ 6 :- 7 :+ 8 :* 9)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment