Skip to content

Instantly share code, notes, and snippets.

@ctrlrsf
Last active January 3, 2016 08:49
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 ctrlrsf/12ea523e63539f166375 to your computer and use it in GitHub Desktop.
Save ctrlrsf/12ea523e63539f166375 to your computer and use it in GitHub Desktop.
(defn numofzeros
"Returns number of zeros in a number. Eg, 1000 returns 3."
[input]
(count (filter #(= \0 %) (str input))))
(defn countall
"Count all number of zeros contained in all numbers between 0 and n (inclusive)
Returns map of number of zeros in n and sum of all combined zeros"
[n]
(let [lastnumzeros (numofzeros n)
sum (reduce (fn [acc x] (+ acc (numofzeros x))) 0 (range (inc n)))]
{:zeros lastnumzeros :sum sum}))
(println (countall (long 1e9))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment