Skip to content

Instantly share code, notes, and snippets.

@jeremyrsellars
Created September 24, 2014 18:21
Show Gist options
  • Save jeremyrsellars/072e8f1604ce60898904 to your computer and use it in GitHub Desktop.
Save jeremyrsellars/072e8f1604ce60898904 to your computer and use it in GitHub Desktop.
Does this base-10 number contain the digit 7? (a career fair question solution)
(defn contains-7? [number]
(if (zero? number) false
(letfn [(seven? [n]
(== 7 n))
(one-digit? [n]
(< n 10))
(leading-digit [n]
(int
(if (one-digit? n)
n
(leading-digit (/ n 10)))))]
(let [place-holder-digits (int (Math/log10 number))
ld (leading-digit number)]
(if (seven? ld) true
(recur (- number (* ld (Math/pow 10 place-holder-digits)))))))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment