Guest User

http://programmingpraxis.com/2012/06/15/counting-ones/

a guest
Jun 15th, 2012
132
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lisp 0.27 KB | None | 0 0
  1. (defun digits (n &optional (b 10))
  2.   (loop for i = n then (floor i b) with lst = ()
  3.      do (push (mod i b) lst)
  4.      while (> i (1- b))
  5.      finally (return lst)))
  6.  
  7. (defun onep (n) (equal 1 n))
  8. (defun count-ones (n)
  9.   (reduce #'+ (remove-if-not #'onep (digits n))))
Add Comment
Please, Sign In to add comment