1 2 32 4 512 by hebiyan

(defun prefixp (llst rlst)
  (cond
    ((or (null llst) (null rlst)) T)
    ((equal (first llst) (first rlst))
     (prefixp (cdr llst) (cdr rlst)))
    (t nil)))

(defun start-with (s1 s2)
  (prefixp (map 'list #'identity s1)
           (map 'list #'identity s2)))

(defun find-power-of-2 (n)
  (let ((start-n (floor (log n 2)))
        (ns (format nil "~a" n)))
    (loop for m = start-n then (1+ m)
       for p = (expt 2 m)
       if (start-with (format nil "~a" p) ns)
       return p)))

(defun main ()
  (format t "~{~a~%~}"
          (loop for n from 1 upto 100
             collect (find-power-of-2 n))))
(main)

Note that non-ascii characters in the above source code will be escaped (such as \x9f).

download

return to the top page