Tuesday, December 31, 2013

Dealing with extremely big numbers.

Suppose we need to calculate the sum of 2 numbers. Seems pretty easy task but when we are han\ving a look at the constraints whichs say "both numbers are integers up to 10^1000", the task becomes difficult.
   For dealing with big numbers we need to use the knowledge from our 1st grade when teacxhers showed us how to sum up numbers . We need to write them one under another and sum up the digits, if we are getting a number bigger than 9 then we subtract 10 from that number and we keep 1 in mind and then we add that 1 to the second suming of digits.
        12344
     + 56666
     = 69010
So now a little bit about implementation,. We need to use arrays to solve this. We have to keep arrays where every element is a single digit of a given number and then , using a simple loops, we iterate over our array summing up the digit and keeping some numbers for suming up on next turn if necessary.         

No comments:

Post a Comment