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 ?
|
|
0
|
|
|
|
Reply
|
thurahlaing06 (14)
|
12/7/2008 6:13:40 PM |
|
On Dec 7, 12:13=A0pm, thurahlain...@gmail.com wrote:
> I am trying to solve this question.
>
> 2^(15) =3D 32768 and the sum of its digits is 3 + 2 + 7 + 6 + 8 =3D 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))
|
|
0
|
|
|
|
Reply
|
quickbasicguru (68)
|
12/7/2008 6:21:25 PM
|
|
> 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 ...
|
|
0
|
|
|
|
Reply
|
thurahlaing06 (14)
|
12/7/2008 7:41:31 PM
|
|
On Dec 7, 8:41 pm, thurahlain...@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
|
|
0
|
|
|
|
Reply
|
joswig (505)
|
12/7/2008 8:17:30 PM
|
|
On Dec 7, 11:13=A0am, thurahlain...@gmail.com wrote:
> I am trying to solve this question.
>
> 2^(15) =3D 32768 and the sum of its digits is 3 + 2 + 7 + 6 + 8 =3D 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
|
|
0
|
|
|
|
Reply
|
wade.humeniuk (58)
|
12/7/2008 9:58:37 PM
|
|
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
|
|
0
|
|
|
|
Reply
|
tkpapp (975)
|
12/7/2008 10:15:46 PM
|
|
In article
<8abe9b94-c4ed-4df4-8f82-750f1b40c33d@x8g2000yqk.googlegroups.com>,
Wade <wade.humeniuk@gmail.com> wrote:
> 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
If put in a library it might be worth to have recursion
replaced with iteration (-> stack size limit).
--
http://lispm.dyndns.org/
|
|
0
|
|
|
|
Reply
|
joswig8642 (2198)
|
12/7/2008 10:15:55 PM
|
|
On Dec 7, 3:15=A0pm, Rainer Joswig <jos...@lisp.de> wrote:
> In article
> <8abe9b94-c4ed-4df4-8f82-750f1b40c...@x8g2000yqk.googlegroups.com>,
>
>
>
> =A0Wade <wade.humen...@gmail.com> wrote:
> > On Dec 7, 11:13=A0am, thurahlain...@gmail.com wrote:
> > > I am trying to solve this question.
>
> > > 2^(15) =3D 32768 and the sum of its digits is 3 + 2 + 7 + 6 + 8 =3D 2=
6.
> > > 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))
> > =A0 "Returns a list of the digits (base radix) of an integer. =A0Least
> > digit
> > =A0 =A0is in position 0."
> > =A0 (check-type n integer)
> > =A0 (if (zerop n)
> > =A0 =A0 =A0 (list 0)
> > =A0 =A0 =A0 (labels
> > =A0 =A0 =A0((digit-list (n)
> > =A0 =A0 =A0 =A0 (multiple-value-bind (upper digit)
> > =A0 =A0 =A0 =A0 =A0 =A0 (floor n radix)
> > =A0 =A0 =A0 =A0 =A0 (cons digit (when (> upper 0) =A0(digit-list upper)=
)))))
> > =A0 =A0(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
|
|
0
|
|
|
|
Reply
|
wade.humeniuk (58)
|
12/7/2008 10:33:48 PM
|
|
In article
<58423cde-97e0-4f13-ab4d-8d74140068b6@l42g2000yqe.googlegroups.com>,
Wade <wade.humeniuk@gmail.com> wrote:
> On Dec 7, 3:15�pm, Rainer Joswig <jos...@lisp.de> wrote:
> > In article
> > <8abe9b94-c4ed-4df4-8f82-750f1b40c...@x8g2000yqk.googlegroups.com>,
> >
> >
> >
> > �Wade <wade.humen...@gmail.com> wrote:
> > > 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
> >
> > 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/
|
|
0
|
|
|
|
Reply
|
joswig8642 (2198)
|
12/7/2008 10:54:53 PM
|
|
Tamas K Papp <tkpapp@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@rpw3.org>
627 26th Avenue <URL:http://rpw3.org/>
San Mateo, CA 94403 (650)572-2607
|
|
0
|
|
|
|
Reply
|
rpw3 (2294)
|
12/8/2008 5:07:49 AM
|
|
* (Rob Warnock) <Z7-dnfnvaPE4NqHUnZ2dnUVZ_sbinZ2d@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
|
|
0
|
|
|
|
Reply
|
enometh (820)
|
12/8/2008 5:29:24 AM
|
|
thurahlaing06@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__
|
|
0
|
|
|
|
Reply
|
pjb (7643)
|
12/8/2008 9:36:04 AM
|
|
Thanks you all ...
Sure, I will use digit-char-p ...
I don't use because I don't know that function exists !
|
|
0
|
|
|
|
Reply
|
thurahlaing06 (14)
|
12/8/2008 5:08:11 PM
|
|
|
12 Replies
27 Views
(page loaded in 0.22 seconds)
|