Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

how to cast integer into string ?

374 views
Skip to first unread message

thurah...@gmail.com

unread,
Dec 7, 2008, 1:13:40 PM12/7/08
to

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 ?

Brian

unread,
Dec 7, 2008, 1:21:25 PM12/7/08
to
One way: (prin1-to-string (expt 2 1000))

thurah...@gmail.com

unread,
Dec 7, 2008, 2:41:31 PM12/7/08
to

> 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 ...

jos...@corporate-world.lisp.de

unread,
Dec 7, 2008, 3:17:30 PM12/7/08
to

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

Wade

unread,
Dec 7, 2008, 4:58:37 PM12/7/08
to
On Dec 7, 11:13 am, thurahlain...@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

Tamas K Papp

unread,
Dec 7, 2008, 5:15:46 PM12/7/08
to

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

Rainer Joswig

unread,
Dec 7, 2008, 5:15:55 PM12/7/08
to
In article
<8abe9b94-c4ed-4df4...@x8g2000yqk.googlegroups.com>,
Wade <wade.h...@gmail.com> wrote:

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

--
http://lispm.dyndns.org/

Wade

unread,
Dec 7, 2008, 5:33:48 PM12/7/08
to
On Dec 7, 3:15 pm, Rainer Joswig <jos...@lisp.de> wrote:
> In article
> <8abe9b94-c4ed-4df4-8f82-750f1b40c...@x8g2000yqk.googlegroups.com>,

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

Rainer Joswig

unread,
Dec 7, 2008, 5:54:53 PM12/7/08
to
In article
<58423cde-97e0-4f13...@l42g2000yqe.googlegroups.com>,
Wade <wade.h...@gmail.com> wrote:

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/

Rob Warnock

unread,
Dec 8, 2008, 12:07:49 AM12/8/08
to
Tamas K Papp <tkp...@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 <rp...@rpw3.org>
627 26th Avenue <URL:http://rpw3.org/>
San Mateo, CA 94403 (650)572-2607

Madhu

unread,
Dec 8, 2008, 12:29:24 AM12/8/08
to

* (Rob Warnock) <Z7-dnfnvaPE4NqHU...@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

Pascal J. Bourguignon

unread,
Dec 8, 2008, 4:36:04 AM12/8/08
to
thurah...@gmail.com writes:

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__

thurah...@gmail.com

unread,
Dec 8, 2008, 12:08:11 PM12/8/08
to
Thanks you all ...
Sure, I will use digit-char-p ...
I don't use because I don't know that function exists !


0 new messages