Bignum arithmetic

Let's talk about algorithms!

Moderator: Board moderators

Post Reply
marcink86
New poster
Posts: 5
Joined: Fri Mar 10, 2006 1:41 am

Bignum arithmetic

Post by marcink86 »

Hi!

I'm facing up a bignum problems right now. I tried to write substraction, addition, multiplication and division on bignums. However, coding multiplication and division took me too long and I'm not still able to write it. Substraction and addition with carry operations can be easily written.

So far, I know that the best way to store bugnums are tables of longs (C++) where the number is read from right to left i.e. number = 1234567891011
t[0]=67891011 t[1]=12345
Is there anything better?

Could anybody tell me how to multiply and divide bignums (BIG1*BIG2 and BIG1/BIG2) as fast as it it possible?

I would be grateful for any useful pieces of advice. Thanks in advance for reply.
donatello
New poster
Posts: 8
Joined: Tue Mar 07, 2006 3:08 pm

Post by donatello »

The easiest multiplication is O(n squared), but if you want to go for speed look up karatsuba multiplication on google or mathworld. But I think they are horribly difficult to code though worth the trouble for the speed improvement you get for really large numbers [O(n raised to 1.58 ) if I remember right. ]
marcink86
New poster
Posts: 5
Joined: Fri Mar 10, 2006 1:41 am

Post by marcink86 »

And what about division?
Artikali
Learning poster
Posts: 68
Joined: Wed Sep 21, 2005 5:27 pm

Post by Artikali »

chunyi81
A great helper
Posts: 293
Joined: Sat Jun 21, 2003 4:19 am
Location: Singapore

Post by chunyi81 »

the BigInteger library in Steven Halim's website does not work properly for division of 2 big integers. But it does work quite ok if u divide a big integer by a constant
marcink86
New poster
Posts: 5
Joined: Fri Mar 10, 2006 1:41 am

Post by marcink86 »

Unfortunately, those libraries do not satisfy me. I'd like to know how it's done and which algo has been used.
chunyi81
A great helper
Posts: 293
Joined: Sat Jun 21, 2003 4:19 am
Location: Singapore

Post by chunyi81 »

Think about the paper and pen method of division.
marcink86
New poster
Posts: 5
Joined: Fri Mar 10, 2006 1:41 am

Post by marcink86 »

In fact, it's too slow. I need to know faster algo. Please help me!
david
Learning poster
Posts: 83
Joined: Mon Apr 21, 2003 10:14 pm

Post by david »

Both multiplication and division can be done in O(n^2) without much trouble.
The so-called "karatsuba algorithm" (O(n^(log 3 / log2)) is just the divide-and-conquer method for integer multiplication found in every textbook. It isn't difficult to code, but as far as I know the gain in speed is only noticeable when multiplying numbers with several thousand digits.
As for division, you can use a variant of the paper-and-pencil method. The only difficulty is determining what the next "digit" (in a large base, such as 10^9) will be. You can use the result of dividing the first two digits of the dividend by the first digit of the divisor as an initial estimate for the next digit.
If the first "digit" of the dividend is no smaller than half the base, it can easily be shown that this estimate is at most four units away from the true result, so you only need to check at most four values. So the key to make this method work fast is to first multiply both dividend and divisor by a constant, so that the first digit of the divisor becomes >= base / 2.
I suppose none of these algorithms are the state of the art for integer arithmetics, but they work reasonably fast. I don't know what you intend to do, though.
ferrizzi
New poster
Posts: 23
Joined: Thu Mar 30, 2006 5:42 pm
Location: Brazil

Post by ferrizzi »

I use string representation to deal with bigNums. A poblem that requires it it's the "How Many Fibs?" problem. I used character representation, i think it's more easy :)
But if you are using huge numbers, I don't know if it works hehe, cause a vector has a limit to be allocated..
Regards,
[]'s
Andre
Post Reply

Return to “Algorithms”