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-18 11:12

C# runs in a managed GC-ed environment. It really does NOT matter if he is using int.

Hey >>1-chan, if you wanna look a little cooler, you can use a short array. They use a little less bytes than a regular int and probably won't mess up your code.

As for the subtraction, make a routine that thinks on something like this:

240
-45


I am assuming you are using 2 arrays, and the answer will be in the first one, where is the number that is being subtracted on.

"well, 2 minus 0 is two. next!
4 minus 4 is 0. next!
0 minus 5? oops. I am going to borrow from the house directly above.
There is 0? I can't take from 0. Oops. I am going to borrow from the house directly above.
There is 2? Great. Subtract one from this, add 10 to the inferior house, then go back to the inferior house //use recursion to achieve this.
There is 10? Great. Subtract one from this, add 10 to the inferior house, then go back to the inferior house.
10 minus 5 is 5.
No nexts? Print the result.

Name: Anonymous 2012-02-18 13:28

>>15
I'm not him, but that's easy. Just check if n2 is bigger than n1, and if true, do the reverse order subtraction and switch the + to a - (a signature bit or whatever you are using).

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