Newsgroups: comp.lang.lisp
Path: cantaloupe.srv.cs.cmu.edu!rochester!cornellcs!newsstand.cit.cornell.edu!news.kei.com!newsfeed.internetmci.com!in1.uu.net!brighton.openmarket.com!decwrl!waikato!comp.vuw.ac.nz!actrix.gen.nz!news
From: padams@actrix.gen.nz (Peter Adams)
Subject: Re: How do I calculate 2^850000 and see all the digits?
Message-ID: <DJGBEI.GKA@actrix.gen.nz>
Sender: news@actrix.gen.nz (News Administrator)
Organization: Actrix Networks -- NZ Internet Service Providers.
Date: Tue, 12 Dec 1995 03:09:47 GMT
References: <4afc4g$nsk@news.infi.net>
X-Newsreader: Forte Free Agent 1.0.82
X-Nntp-Posting-Host: taumaru.actrix.gen.nz
Lines: 54

bpatters@pcs.cnu.edu (Blake Patterson) wrote:


>	I understand this _is_ possible under gcl with 'bignums.'  I
>lack significant documentation of the language -- can anyone show me
>some example code where bignums are used?  I need to (in case you missed
>the topic) print out all the decimals in the computation of 2^850000.

>	Thanks.

>	Email is supernice.

>bp
>-- 
>                                [[[ URL: http://www.pcs.cnu.edu/~bpatters ]]]
>+--------------------------------- ---- -- -  -   -     -           -
>|Blake W. Patterson         "I'm not quite clear about what you just spoke-
>|bpatters@pcs.cnu.edu        Was that a parable, or a very subtle joke?"   
>|blake@vigsun1.larc.nasa.gov                                              
>+--ToriAmos-LoreenaMcKennitt-Enya-DavidWilcox-SarahMcLachlan-ElvisCostello--+
>+----DavidBowie-CrashTestDummies-TheyMightBeGiants-RustedRoot-Pixies-XTC----+

If you want to see the result as a binary number it is simply 1
followed by 850000 zeros. You don't even need a Lisp program to tell
you that 8-)

The answer in decimal form would be 255,876 digits long (very nearly
60 pages at 60 lines to the page, and 72 digits to the line).

Here are two ways to calculate the answer in bignums. They work in ACL
so I preume the answer would be the same with GCL:

(defun pwr1 (lim)
   (do ((n 1) (count 0)) ((>= count lim) n)
      (setq n (* 2 n))
      (setq count (1+ count))))
;;
;;
(defun pwr2 (n)
   (cond
         ((= n 2) 4)
         ((oddp n) (* (pwr2 (- n 1)) 2))
         (t (square (pwr2 (/ n 2))))))
;;
;;
;;
I hope that your computer doesn't choke if you try to run the above
code using an argument value of 850,000 8-(.

Regards,


Peter Adams

