Saturday, August 15, 2009

Euler Project 16

215 = 32768 and the sum of its digits is 3 + 2 + 7 + 6 + 8 = 26.

What is the sum of the digits of the number 21000?


My Solution
(defun Euler16()
  (let* ((numberstr (format nil "~a" (expt 2 1000))))
(loop for i from 0 to (- (length numberstr) 1)
     collect (parse-integer numberstr :start i :end (+ i 1)))))
(apply #'+ (Euler16))

Others' Solutions
First Solution
(defun num-of-char (c) 
(- (char-int c) 48)) 

(defun euler-16() 
(apply #'+ 
(map 'list #'num-of-char (format nil "~d" (expt 2 1000))))) 

Second Solution
(defun sum-number-digits (n) 
        (if (zerop n)
0
(+ (mod n 10) (sum-number-digits (/ (- n (mod n 10)) 10)))))
 
(sum-number-digits (expt 2 1000))

Third Solution



No comments:

Post a Comment