Lisp Binary Gap
(defun binary-gap (n)
(let* ((bits (write-to-string n :base 2))
(trimmed (string-trim "0" bits))
(groups (split-on-char trimmed #\1))
(gap 0))
(dolist (zero groups)
(setf gap (max gap (length zero))))
gap))
(defun split-on-char (s ch)
(loop with start = 0
for pos = (position ch s :start start)
collect (subseq s start pos)
while pos
do (setf start (1+ pos))))
This turns the number into binary, ignores zeroes outside the edges, and finds the longest run of zeroes between 1s.