Created
July 22, 2013 16:04
-
-
Save emasaka/6055058 to your computer and use it in GitHub Desktop.
my answer to https://codeiq.jp/ace/chikamune_minoru/q382
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
(defn encoded-num-seq [n digits] | |
(let [base (.length digits) | |
enc1 (fn [n r] | |
(let [q (quot n base) r2 (str (.charAt digits (rem n base)) r)] | |
(if (zero? q) r2 (recur q r2)) ))] | |
(map #(enc1 % "") (iterate inc n)) )) | |
(defmacro nth [c i & nf] | |
(if (and (list? c) (= (first c) 'encoded-num-seq)) | |
(let [(f a1 a2) c] `(first (~f (+ ~i ~a1) ~a2))) | |
`(clojure.core/nth ~c ~i ~@nf) )) |
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 enc1(n, digits, base) | |
r = '' | |
begin | |
r.prepend digits[n % base] | |
end while (n /= base) > 0 | |
r | |
end | |
def encoded_num_seq(n, digits) | |
base = digits.size | |
(n..Float::INFINITY).lazy.map{|x| enc1(x, digits, base) } | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment