Skip to content

Instantly share code, notes, and snippets.

@mihi-tr
Last active December 16, 2015 13:39
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 mihi-tr/5443534 to your computer and use it in GitHub Desktop.
Save mihi-tr/5443534 to your computer and use it in GitHub Desktop.
(def coins [2 1 1 0.5 0.5 0.2 0.2 0.2 0.1 0.05 0.05 0.02 0.02 0.01 0.01])
(defn square [x]
(* x x))
(defn distance
[x y]
(square (- x y)))
(defn fix-coin
[i coins fixed]
(let [coins (reverse (sort coins))]
(loop [coins coins
fixed fixed]
(if (and (first coins) (> (first coins) i))
(recur (rest coins) (conj fixed (first coins)))
[coins, fixed]))))
(defn maximum-coins
[sum coins]
(let [sum (int (* 100 sum))]
(loop [cns (map #(int (* 100 %)) coins)
give '()]
(let [s (apply + (concat cns give))
d (- s sum)
[cns give] (fix-coin d cns give)
cns (sort #(compare (distance d %1) (distance d %2))
cns)]
(if (> (count cns) 0)
(recur (rest cns) give)
(map #(float (/ % 100)) give))))))
@mihi-tr
Copy link
Author

mihi-tr commented Apr 23, 2013

The initial version was simpler - this works however....

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment