Skip to content

Instantly share code, notes, and snippets.

@jaked
Last active December 20, 2015 09:48
Show Gist options
  • Save jaked/6110141 to your computer and use it in GitHub Desktop.
Save jaked/6110141 to your computer and use it in GitHub Desktop.
(defn count-ways-to-make-change [coins sum]
(defn sum-counts [counts]
(reduce + (map * coins counts)))
(defn next-counts [counts]
(loop [counts counts
digit 0]
(if (>= digit (count counts))
nil
(let [next (assoc counts digit (inc (get counts digit)))]
(if (<= (sum-counts next) sum)
next
(recur (assoc counts digit 0) (inc digit)))))))
(loop [counts (vec (map (constantly 0) coins))
ways 0]
(if (nil? counts)
ways
(let [sum-counts (sum-counts counts)
ways (if (= sum-counts sum) (inc ways) ways)
counts (next-counts counts)]
(recur counts ways)))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment