Skip to content

Instantly share code, notes, and snippets.

@Sophia-Gold
Created August 18, 2017 03:53
Show Gist options
  • Save Sophia-Gold/fbf1e642d9d2720236f44309df170cbd to your computer and use it in GitHub Desktop.
Save Sophia-Gold/fbf1e642d9d2720236f44309df170cbd to your computer and use it in GitHub Desktop.
using binary method (not always shortest)
(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