From: ·············@gmail.com
Subject: how to cast integer into string ?
Date: 
Message-ID: <2d97d2a7-f5a3-4cdf-a3b3-c884d4b0e600@r15g2000prd.googlegroups.com>
I am trying to solve this question.

2^(15) = 32768 and the sum of its digits is 3 + 2 + 7 + 6 + 8 = 26.
What is the sum of the digits of the number 2^(1000)?

This is how I solved in python.
sum([int(x) for x in str(pow(2,1000))])

For lisp, I don't know how to implement that algorithm ...
This is my sketch on how to slove ...

(loop for x across "12345" summing (- (char-int x) 48))

Still I need to cast (expt 2 1000) into string from integer ...
How can I do that ?

From: Brian
Subject: Re: how to cast integer into string ?
Date: 
Message-ID: <07d0c96a-4ed3-455d-96aa-e93a8b75b14a@h5g2000yqh.googlegroups.com>
On Dec 7, 12:13 pm, ·············@gmail.com wrote:
> I am trying to solve this question.
>
> 2^(15) = 32768 and the sum of its digits is 3 + 2 + 7 + 6 + 8 = 26.
> What is the sum of the digits of the number 2^(1000)?
>
> This is how I solved in python.
> sum([int(x) for x in str(pow(2,1000))])
>
> For lisp, I don't know how to implement that algorithm ...
> This is my sketch on how to slove ...
>
> (loop for x across "12345" summing (- (char-int x) 48))
>
> Still I need to cast (expt 2 1000) into string from integer ...
> How can I do that ?
One way: (prin1-to-string (expt 2 1000))
From: ·············@gmail.com
Subject: Re: how to cast integer into string ?
Date: 
Message-ID: <f9207162-efb2-4ce9-afcf-e22553bdf847@p2g2000prn.googlegroups.com>
> One way: (prin1-to-string (expt 2 1000))
 (loop for x across (prin1-to-string (expt 2 1000)) summing (- (char-
int x) 48))

It works ! Thanks ...
From: ······@corporate-world.lisp.de
Subject: Re: how to cast integer into string ?
Date: 
Message-ID: <613e9c07-d4ec-40d4-935f-ffb188927910@w35g2000yqm.googlegroups.com>
On Dec 7, 8:41 pm, ·············@gmail.com wrote:
> > One way: (prin1-to-string (expt 2 1000))
>
>  (loop for x across (prin1-to-string (expt 2 1000)) summing (- (char-
> int x) 48))
>
> It works ! Thanks ...

It would be slightly better to use CHAR-CODE. But you can also use
DIGIT-CHAR-P.

CL-USER 4 > (digit-char-p #\3)
3

CL-USER 5 > (reduce #'+ (write-to-string (expt 2 1000)) :key #'digit-
char-p)
1366
From: Pascal J. Bourguignon
Subject: Re: how to cast integer into string ?
Date: 
Message-ID: <7ctz9f80vv.fsf@pbourguignon.anevia.com>
·············@gmail.com writes:

>> One way: (prin1-to-string (expt 2 1000))
>  (loop for x across (prin1-to-string (expt 2 1000)) summing (- (char-
> int x) 48))
>
> It works ! Thanks ...

No it doesn't work here (EBCDIC system).

Use: (loop :for x :across (prin1-to-string (expt 2 1000)) :summing (digit-char-p x))

Yes, DIGIT-CHAR-P returns what you want, despite of its name.

-- 
__Pascal Bourguignon__
From: ·············@gmail.com
Subject: Re: how to cast integer into string ?
Date: 
Message-ID: <23541853-adc4-4cc5-a9e2-2d4235baab15@p2g2000prn.googlegroups.com>
Thanks you all ...
Sure, I will use digit-char-p ...
I don't use because  I don't know that function exists !
From: Wade
Subject: Re: how to cast integer into string ?
Date: 
Message-ID: <8abe9b94-c4ed-4df4-8f82-750f1b40c33d@x8g2000yqk.googlegroups.com>
On Dec 7, 11:13 am, ·············@gmail.com wrote:
> I am trying to solve this question.
>
> 2^(15) = 32768 and the sum of its digits is 3 + 2 + 7 + 6 + 8 = 26.
> What is the sum of the digits of the number 2^(1000)?
>
> This is how I solved in python.
> sum([int(x) for x in str(pow(2,1000))])
>
> For lisp, I don't know how to implement that algorithm ...
> This is my sketch on how to slove ...
>
> (loop for x across "12345" summing (- (char-int x) 48))

As an alternative,

(defun integer-digit-list (n &optional (radix 10))
  "Returns a list of the digits (base radix) of an integer.  Least
digit
   is in position 0."
  (check-type n integer)
  (if (zerop n)
      (list 0)
      (labels
	  ((digit-list (n)
	     (multiple-value-bind (upper digit)
		 (floor n radix)
	       (cons digit (when (> upper 0)  (digit-list upper))))))
	(digit-list (abs n)))))

CL-USER> (reduce '+ (integer-digit-list (expt 2 1000)))
1366
CL-USER> (reduce '+ (integer-digit-list (expt 2 1000) 16))
1
CL-USER>

Wade
From: Rainer Joswig
Subject: Re: how to cast integer into string ?
Date: 
Message-ID: <joswig-EB5682.23155507122008@news-europe.giganews.com>
In article 
<····································@x8g2000yqk.googlegroups.com>,
 Wade <·············@gmail.com> wrote:

> On Dec 7, 11:13�am, ·············@gmail.com wrote:
> > I am trying to solve this question.
> >
> > 2^(15) = 32768 and the sum of its digits is 3 + 2 + 7 + 6 + 8 = 26.
> > What is the sum of the digits of the number 2^(1000)?
> >
> > This is how I solved in python.
> > sum([int(x) for x in str(pow(2,1000))])
> >
> > For lisp, I don't know how to implement that algorithm ...
> > This is my sketch on how to slove ...
> >
> > (loop for x across "12345" summing (- (char-int x) 48))
> 
> As an alternative,
> 
> (defun integer-digit-list (n &optional (radix 10))
>   "Returns a list of the digits (base radix) of an integer.  Least
> digit
>    is in position 0."
>   (check-type n integer)
>   (if (zerop n)
>       (list 0)
>       (labels
> 	  ((digit-list (n)
> 	     (multiple-value-bind (upper digit)
> 		 (floor n radix)
> 	       (cons digit (when (> upper 0)  (digit-list upper))))))
> 	(digit-list (abs n)))))
> 
> CL-USER> (reduce '+ (integer-digit-list (expt 2 1000)))
> 1366
> CL-USER> (reduce '+ (integer-digit-list (expt 2 1000) 16))
> 1
> CL-USER>
> 
> Wade

If put in a library it might be worth to have recursion
replaced with iteration (-> stack size limit).

-- 
http://lispm.dyndns.org/
From: Wade
Subject: Re: how to cast integer into string ?
Date: 
Message-ID: <58423cde-97e0-4f13-ab4d-8d74140068b6@l42g2000yqe.googlegroups.com>
On Dec 7, 3:15 pm, Rainer Joswig <······@lisp.de> wrote:
> In article
> <····································@x8g2000yqk.googlegroups.com>,
>
>
>
>  Wade <·············@gmail.com> wrote:
> > On Dec 7, 11:13 am, ·············@gmail.com wrote:
> > > I am trying to solve this question.
>
> > > 2^(15) = 32768 and the sum of its digits is 3 + 2 + 7 + 6 + 8 = 26.
> > > What is the sum of the digits of the number 2^(1000)?
>
> > > This is how I solved in python.
> > > sum([int(x) for x in str(pow(2,1000))])
>
> > > For lisp, I don't know how to implement that algorithm ...
> > > This is my sketch on how to slove ...
>
> > > (loop for x across "12345" summing (- (char-int x) 48))
>
> > As an alternative,
>
> > (defun integer-digit-list (n &optional (radix 10))
> >   "Returns a list of the digits (base radix) of an integer.  Least
> > digit
> >    is in position 0."
> >   (check-type n integer)
> >   (if (zerop n)
> >       (list 0)
> >       (labels
> >      ((digit-list (n)
> >         (multiple-value-bind (upper digit)
> >             (floor n radix)
> >           (cons digit (when (> upper 0)  (digit-list upper))))))
> >    (digit-list (abs n)))))
>
> > CL-USER> (reduce '+ (integer-digit-list (expt 2 1000)))
> > 1366
> > CL-USER> (reduce '+ (integer-digit-list (expt 2 1000) 16))
> > 1
> > CL-USER>
>
> > Wade
>
> If put in a library it might be worth to have recursion
> replaced with iteration (-> stack size limit).
>

Here is a iterative solution.

(defun integer-digit-list (n &optional (radix 10))
  "Returns a list of the digits base radix of an integer.  Least digit
   is in position 0."
  (check-type n integer)
  (setf n (abs n))
  (loop collect
       (multiple-value-bind (upper digit)
	   (floor n radix)
	 (setf n upper)
	 digit)
     while (> n 0)))

Wade
From: Rainer Joswig
Subject: Re: how to cast integer into string ?
Date: 
Message-ID: <joswig-431A92.23545207122008@news-europe.giganews.com>
In article 
<····································@l42g2000yqe.googlegroups.com>,
 Wade <·············@gmail.com> wrote:

> On Dec 7, 3:15�pm, Rainer Joswig <······@lisp.de> wrote:
> > In article
> > <····································@x8g2000yqk.googlegroups.com>,
> >
> >
> >
> > �Wade <·············@gmail.com> wrote:
> > > On Dec 7, 11:13�am, ·············@gmail.com wrote:
> > > > I am trying to solve this question.
> >
> > > > 2^(15) = 32768 and the sum of its digits is 3 + 2 + 7 + 6 + 8 = 26.
> > > > What is the sum of the digits of the number 2^(1000)?
> >
> > > > This is how I solved in python.
> > > > sum([int(x) for x in str(pow(2,1000))])
> >
> > > > For lisp, I don't know how to implement that algorithm ...
> > > > This is my sketch on how to slove ...
> >
> > > > (loop for x across "12345" summing (- (char-int x) 48))
> >
> > > As an alternative,
> >
> > > (defun integer-digit-list (n &optional (radix 10))
> > > � "Returns a list of the digits (base radix) of an integer. �Least
> > > digit
> > > � �is in position 0."
> > > � (check-type n integer)
> > > � (if (zerop n)
> > > � � � (list 0)
> > > � � � (labels
> > > � � �((digit-list (n)
> > > � � � � (multiple-value-bind (upper digit)
> > > � � � � � � (floor n radix)
> > > � � � � � (cons digit (when (> upper 0) �(digit-list upper))))))
> > > � �(digit-list (abs n)))))
> >
> > > CL-USER> (reduce '+ (integer-digit-list (expt 2 1000)))
> > > 1366
> > > CL-USER> (reduce '+ (integer-digit-list (expt 2 1000) 16))
> > > 1
> > > CL-USER>
> >
> > > Wade
> >
> > If put in a library it might be worth to have recursion
> > replaced with iteration (-> stack size limit).
> >
> 
> Here is a iterative solution.
> 
> (defun integer-digit-list (n &optional (radix 10))
>   "Returns a list of the digits base radix of an integer.  Least digit
>    is in position 0."
>   (check-type n integer)
>   (setf n (abs n))
>   (loop collect
>        (multiple-value-bind (upper digit)
> 	   (floor n radix)
> 	 (setf n upper)
> 	 digit)
>      while (> n 0)))
> 
> Wade

Yep.

or using SETF instead of MULTIPLE-VALUE-BIND:

(defun integer-digit-list (n &optional (radix 10))
  "Returns a list of the digits base radix of an integer.  Least digit
   is in position 0."
  (check-type n integer)
  (setf n (abs n))
  (loop with digit
        do (setf (values n digit) (floor n radix))
        collect digit while (plusp n)))

-- 
http://lispm.dyndns.org/
From: Tamas K Papp
Subject: Re: how to cast integer into string ?
Date: 
Message-ID: <6q308iFaem6lU2@mid.individual.net>
On Sun, 07 Dec 2008 10:13:40 -0800, thurahlaing06 wrote:

> I am trying to solve this question.
> 
> 2^(15) = 32768 and the sum of its digits is 3 + 2 + 7 + 6 + 8 = 26. What
> is the sum of the digits of the number 2^(1000)?
> 
> This is how I solved in python.
> sum([int(x) for x in str(pow(2,1000))])
> 
> For lisp, I don't know how to implement that algorithm ... This is my
> sketch on how to slove ...
> 
> (loop for x across "12345" summing (- (char-int x) 48))
> 
> Still I need to cast (expt 2 1000) into string from integer ... How can
> I do that ?

Others have already provided solutions.  I wanted to add mine because it 
is important to realize that you do not need the intermediate list of 
digits, and you can proceed from either end (+ is commutative).  So

(defun sum-digits (i)
  (declare ((integer 0) i)) ;; we want a nonnegative integer
  (let ((sum 0))
    (tagbody
     top
       (multiple-value-bind (quotient remainder) (truncate i 10)
	 (incf sum remainder)
	 (when (plusp quotient)
	   (setf i quotient)
	   (go top))))
    sum))

(sum-digits (expt 2 1000))

Some consider tagbody ugly - it is their loss :-)

Tamas
From: Rob Warnock
Subject: Re: how to cast integer into string ?
Date: 
Message-ID: <Z7-dnfnvaPE4NqHUnZ2dnUVZ_sbinZ2d@speakeasy.net>
Tamas K Papp  <······@gmail.com> wrote:
+---------------
| Others have already provided solutions.  I wanted to add mine because it 
| is important to realize that you do not need the intermediate list of 
| digits, and you can proceed from either end (+ is commutative).  So
| 
| (defun sum-digits (i)
|   (declare ((integer 0) i)) ;; we want a nonnegative integer
|   (let ((sum 0))
|     (tagbody
|      top
|        (multiple-value-bind (quotient remainder) (truncate i 10)
| 	 (incf sum remainder)
| 	 (when (plusp quotient)
| 	   (setf i quotient)
| 	   (go top))))
|     sum))
| 
| (sum-digits (expt 2 1000))
| 
| Some consider tagbody ugly - it is their loss :-)
+---------------

I cheerfully use TAGBODY when it's appropriate, but I don't consider
the above to be such a case, especially given that there's a considerably
simpler way *without* it, viz.:

    (defun sum-digits (i)
      (assert (and (integerp i) (>= i 0)))
      (loop while (plusp i)
	    summing (multiple-value-bind (quotient remainder)
			(truncate i 10)
		      (setf i quotient)
		      remainder)))


-Rob

p.s. Note that I changed your DECLARE to an ASSERT, since DECLARE is
a promise *by* the programmer, not a check *of* the programmer/user!
I suppose I could have written (ASSERT (TYPEP I '(INTEGER 0 *))),
but the TYPEP is likely to be slower.

-----
Rob Warnock			<····@rpw3.org>
627 26th Avenue			<URL:http://rpw3.org/>
San Mateo, CA 94403		(650)572-2607
From: Madhu
Subject: Re: how to cast integer into string ?
Date: 
Message-ID: <m3tz9fck0b.fsf@moon.robolove.meer.net>
* (Rob Warnock) <································@speakeasy.net> :
Wrote on Sun, 07 Dec 2008 23:07:49 -0600:

| I cheerfully use TAGBODY when it's appropriate, but I don't consider
| the above to be such a case, especially given that there's a considerably
| simpler way *without* it, viz.:
|
|     (defun sum-digits (i)
|       (assert (and (integerp i) (>= i 0)))
|       (loop while (plusp i)
| 	    summing (multiple-value-bind (quotient remainder)
| 			(truncate i 10)
| 		      (setf i quotient)
| 		      remainder)))
|
|
| p.s. Note that I changed your DECLARE to an ASSERT, since DECLARE is
| a promise *by* the programmer, not a check *of* the programmer/user!
| I suppose I could have written (ASSERT (TYPEP I '(INTEGER 0 *))),
| but the TYPEP is likely to be slower.


Since we're picking nits anyway :-} you could use

	(check-type i (integer 0))

instead of the assert 
--
Madhu