Sunday, September 27, 2009

Convert Number to Digits

In Mathematica, there is a useful function which is used to convert number to digits IntegerDigits. I think we also can use the CL to do it.

One Solution


(defun digits (n &optional (base 10)) 

   (do ((digits '())) ((zerop n) digits) 

     (multiple-value-bind (quotient remainder) (truncate n base) 

       (push remainder digits) 

       (setf n quotient)))) 



Two Solution


(map 'list #'digit-char-p (prin1-to-string 15754548)) 


Or


(map 'list #'digit-char-p (format nil "~D" 15754548)


BTW:Thanks the help from Comp.Lang.Lisp

No comments:

Post a Comment