The standard long addition method:
The standard algorithm for adding multidigit numbers is to align the addends vertically and add the columns, starting from the ones column on the right. If a column exceeds ten, the extra digit is "carried" into the next column. (wikipedia)
A long addition example:
145
+ 98
-----
243
Backwards long addition is similar but you start adding from the leftmost column and carry to the next column (on it's right). If the last (ones) column produces a carry you write it behind the current sum in the next column. If there is no carry in the last column no extra column is needed.
145
+ 98
-----
1341
Explanation for the above example by columns, left to right: 1 = 1; 4+9 = 10(carried) + 3; 5+8+1(carry) = 10(carried) + 4; 1(carry) = 1
You should write a program or function which receives two positive integers as input and returns their sum using backwards long addition.
Input
- Two positive integers.
Output
- An integer, the sum of the two input numbers using backwards long addition.
- There should be no leading zeros. (Consider
5+5=1
or73+33=7
.)
Examples
Format is Input => Output
.
1 3 => 4
5 5 => 1
8 9 => 71
22 58 => 701
73 33 => 7
145 98 => 1341
999 1 => 9901
5729 7812 => 26411
L
for numbers greater than2**31
that don't fit in anint
, and so messing up the method of string reversing? \$\endgroup\$2**31
numbers so you have to deal with their representation. \$\endgroup\$