We have four variables:
a, which represents numbers from 0-999b, which representsa1000 (b= 3 representsa= 3000)c, which representsb1000d, which representsc100 (not 1000)
What is the easiest, most efficient or just the standard way of dealing with subtraction/addition to any of those numbers? Imagine I decrease a with 500, while it represents 0, but since b could be 1 (hence a can be 1000) I can and have to recalculate a and b.
I will use $(a,b,c,d)$ to denote the number $a + 10^3 \cdot b + 10^6 \cdot c + 10^8 \cdot d$ (the total amount of "$a$-units"). I will assume that we have $$ 0 \leq a \leq 999 , \quad 0 \leq b \leq 999 , \quad 0 \leq c \leq 99, \quad 0 \leq d. $$ We can calculate the representation
(a,b,c,d)of(a_1,b_1,c_1,d_1) + (a_2,b_2,c_2,d_2)with the process described in the pseudocode below.Of course, all this can be implemented with a loop if we take $a,b,c,d$ as elements of an array.
We can calculate
(a_1,b_1,c_1,d_1) - (a_2,b_2,c_2,d_2)similarly. The first step after initializing variables would beRepeat this until the last step: