Created
August 18, 2017 03:53
-
-
Save Sophia-Gold/fbf1e642d9d2720236f44309df170cbd to your computer and use it in GitHub Desktop.
using binary method (not always shortest)
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 double [i] | |
(bit-shift-left i 1)) | |
(defn to-binary-seq [^long x] | |
(map #(- (int %) (int \0)) | |
(Long/toBinaryString x))) | |
(defn addition-chain [x] | |
(reduce #(if (zero? %2) | |
(conj %1 (double (peek %1))) | |
(conj %1 (double (peek %1)) (inc (double (peek %1))))) | |
[1] | |
(next (to-binary-seq x)))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment