Skip to content

Instantly share code, notes, and snippets.

@jsl
Created June 8, 2012 18:17
Show Gist options
  • Save jsl/2897374 to your computer and use it in GitHub Desktop.
Save jsl/2897374 to your computer and use it in GitHub Desktop.
(ns euler.p8)
(defn ones-digit-of
[number]
(mod number 10))
(defn shift-digit-from
[number]
(/ (- number (ones-digit-of number)) 10))
(defn sample-digits-from
([number to-sample] (sample-digits-from number to-sample []))
([number to-sample sampled]
(if (= (count sampled) to-sample)
sampled
(recur (shift-digit-from number)
to-sample
(cons (ones-digit-of number) sampled)))))
(defn greatest-prod
([number digits] (greatest-prod number digits 0))
([number digits largest]
(if (= number 0)
largest
(let [seq-prod (reduce * (sample-digits-from number digits))
new-largest (apply max [seq-prod largest])]
(recur (shift-digit-from number) digits new-largest)))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment