Skip to content

Instantly share code, notes, and snippets.

@silverprize
Created January 2, 2015 09:43
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 silverprize/400dd3c2be8c36dd2cc1 to your computer and use it in GitHub Desktop.
Save silverprize/400dd3c2be8c36dd2cc1 to your computer and use it in GitHub Desktop.
Count the coins with clojure
(defn- count-change [coins offset target]
(if (= target 0)
1
(loop [coins coins offset offset target target sum 0]
(if (< offset (alength coins))
(if (>= target (nth coins offset))
(recur coins (inc offset) target (+ sum (count-change coins offset (- target (nth coins offset)))))
(recur coins (inc offset) target sum))
sum))))
(let [coins (int-array [1 5 10 25]) target 100]
(print (count-change coins 0 target)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment