Created
September 8, 2019 03:40
-
-
Save kopos/7898d35efb1565473d43a5a1d5243ba3 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(def ^:const KARPEKAR 6174) | |
(defn ->number [ds] | |
(reduce (fn [n d] (+ d (* n 10))) ds)) | |
(defn ->digits [n] | |
(loop [N n | |
ds []] | |
(if (= (count ds) 4) | |
ds | |
(recur (quot N 10) (cons (rem N 10) ds))))) | |
(defn abs [n] (if (< n 0) (- n) n)) | |
(defn kdiff [n] | |
(let [ds (->digits n) | |
a (->number (sort ds)) | |
b (->number (reverse (sort ds)))] | |
(abs (- a b)))) | |
(def nums (for [a (range 10), b (range 10), c (range 10) d (range 10)] | |
[a b c d])) | |
(defn karpekar-seq [n] | |
(loop [n n | |
nums []] | |
(if (or (= n 0) (= n KARPEKAR)) | |
(conj nums n) | |
(recur (kdiff n) (conj nums n))))) | |
(doseq [n nums] | |
(println (-> n ->number karpekar-seq))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment