Skip to content

Instantly share code, notes, and snippets.

Created March 11, 2012 22:50
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/2018527 to your computer and use it in GitHub Desktop.
Save anonymous/2018527 to your computer and use it in GitHub Desktop.
;; ariarule's solution to Digits and bases
;; https://4clojure.com/problem/137
(fn convert-base
([n b]
(if (= n 0) '(0)
(let [p (reverse (take-while #(<= % n) (map #(Math/pow b %) (range))))]
(convert-base '() p n ))))
([l p r]
(if (= (count p) 1)
(reverse (conj l (int r)))
(convert-base (conj l (int (/ r (first p)))) (rest p) (rem r (first p))))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment