Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

Arithmetic with VERY LARGE whole numbers

Name: Anonymous 2012-02-16 0:02

Hi, /prog/.

Was wondering if I could enlist your expertise to help me with this problem.

Basically, I have to write a program)that is capable of performing basic arithmetic with very large numbers (the exact range we were given was -9*10^100 to 9*10^100).

What is supposed to happen is that the user enters string representation of these numbers, then the program converts them to a workable data type, and performs a user defined operation on them (addition, subtraction, multiplication).

My solution has been so far to convert each number into an integer array with each element being a digit in the in the actual large number. (I should probably mention now that I have to do this in C#.)

After both numbers are entered and converted to arrays I basically do the arithmetic digit by digit, elementary-school style.

For example: (addition - [x] represents a value)

Array 1:  .....[5][4][6][7][3] 
Array 2:  .....[3][5][1][1][0]
          _______________________
Result:   .....[8][9][7][8][3]

Obviously, with this method, I would need code that allows the program to "carry the one" when the sum would be more than 1 digit. I already have code that does this.

My problem is with the subtraction. I can't think of a way to make the program "borrow" from another digit in the number, if it needs to. This is what I truly need help with. If you can suggest a way I can implement this OR suggest an entirely different method of subtraction that would work, I would be so, so grateful.

Note that if a class already exists that does this, I'm not allowed to use it. I basically have to re-invent the wheel on this one. I can provide any sample code if you wish.

And I apologize if I wrote way too much. I'm just trying to be as clear as possible,

Thanks.

Name: Anonymous 2012-02-16 6:49

>>8
assuming you work on 64 bit architecture, i guarantee that doing it in base 18446744073709551616 will be much faster, easier and memory efficient than in base 10.

in other words, let your number be represented as array of uint64_t, every element of your array represents a digit of your number.

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List