Hello everybody:

{ I post it here, despite the fact I talk about GHC and Hugs.  I suppose
  this is relevant for most implementations, actual and imagined. }

How can I *efficiently* print (i.e. find the decimal, or in
general N-ary, representation of) large Integers, like factorial of 10000?

The implementation of showInt taken from Standard Prelude:

(1) wastes memory -- both Hugs and GHC2.1 bail out because of insufficient
    heap space (160 megabytes are not enough!)  This can be fixed by adding
    some strictness.  I did it like that ($! added in two places):

    \begin{code}

    showInt n r | n < 0 = error "Numeric.showInt: can't show negative numbers"
                | otherwise =
                  let (n',d) = quotRem n 10
                      r'     = ((:) $! 
                                  (toEnum (fromEnum '0' + fromIntegral d))) r
                  in  if n' == 0 then r' else (showInt n') $! r'

    \end{code}

    though I'm not sure this is the best way.

(2) crawls like hell (when fixed with strictness).
    I don't know whether the slowness is because of GC, or because of
    slowness of quotRem.  Anyway, 100000! can be computed much,
    much faster than printed (using GHC).

So, can printing be done any faster?

I realize that the issue is mostly theoretical.  No one in real
life needs to print numbers so large.
--
Anatoli Tubman <[EMAIL PROTECTED]>


Reply via email to