Re: BigNum -- BigFrac
From: Paul Hsieh (qed_at_pobox.com)
Date: 12/31/03
- Next message: Edward G. Nilges: "Re: Ask for help about wait() for several processes in C ,solaris"
- Previous message: Edward G. Nilges: "Re: Letter to US Sen. Byron Dorgan re unpaid overtime"
- In reply to: Programmer Dude: "Re: BigNum -- BigFrac"
- Next in thread: Programmer Dude: "Re: BigNum -- BigFrac"
- Reply: Programmer Dude: "Re: BigNum -- BigFrac"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: 30 Dec 2003 19:37:15 -0800
Programmer Dude <Chris@Sonnack.com> wrote:
> Richard Heathfield wrote:
> >> ...and 2^(2^16) took over five hours (big number: 19789 digits)!
> >
> > Not quite sure what you're doing wrong there. My lib calculated
> > 2^(2^16) in 0.000 seconds (well, presumably just a /touch/ more
> > than that), and took 119.22 seconds to convert from base 256 to
> > base 10.
>
> Hmmm. I didn't realize I was *that* far from ideal.
Depends on what you mean by ideal. I ressurected an old bigInt
library I was playing with and managed to compute it (well actually I
computed 1 << 65536) in immeasurable time and convert it from base
4294967296 to base 10 in about 0.9 seconds. Of course I cheated and
used a divide-and-conquer mechanism rather than the more obvious
iterative algorithm (its interesting because I can't actually reduce
the number of divides -- but I can reduce the size of the numbers
being divided.)
> > So, er - how come it takes five hours?
>
> Very bad implementation? What do your power and multiply function
> look like (algorithmically)?
For multiply, a good method is to use a column digit by column digit
multiplier. It requires one carry digit register for the upper word
of the multiply, then an additional carry digit register for the
ripple carry of those digit pairs. The advantage of doing it this way
versus the classical public school method is that you don't have to
allocate space for a temporary row.
Power is done by successively squaring and taking those that
corresponds to bits in the exponent as part of the product. For
example:
x ^ 13 = x ^ (8 + 4 + 1)
= x^1 * x^4 * x^8
= (x) * ((x^2)^2) * (((x^2)^2)^2)
You should notice the trivial values of x. For a binary base, that's
0, 1, 2, and any other power of 2. One way or another they can be
computed with some mathematical shortcut or other. If you have a
"lowest bit set on" function and bidirectional shifting, then you can
speed it up by pulling out the power of twos before computing the
result then shifting them back in after multiplying the number by the
exponent.
-- Paul Hsieh http://www.pobox.com/~qed/ http://bstring.sf.net/
- Next message: Edward G. Nilges: "Re: Ask for help about wait() for several processes in C ,solaris"
- Previous message: Edward G. Nilges: "Re: Letter to US Sen. Byron Dorgan re unpaid overtime"
- In reply to: Programmer Dude: "Re: BigNum -- BigFrac"
- Next in thread: Programmer Dude: "Re: BigNum -- BigFrac"
- Reply: Programmer Dude: "Re: BigNum -- BigFrac"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|