Created
January 2, 2015 09:43
-
-
Save silverprize/400dd3c2be8c36dd2cc1 to your computer and use it in GitHub Desktop.
Count the coins with clojure
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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)))) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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
reference