Addition of two whole numbers

Description: This algorithm assumes that its input consists of two positive integers, each represented (using base 10 notation) by a sequence of characters which we will call "digits."

Specifically, we assume the first input number M is represented by the digits
mj mj-1 ··· m2 m1 m0 for some j>=0
and likewise that the second input number N is repesented by digits
nk nk-1 ··· n2 n1 n0 for some k>=0

The resulting sum will also be represented by a sequence of digits,
sl sl-1 ··· s2 s1 s0 for some l>=0

Procedure:

  • Step 1. Assign index i the value 0.

  • Step 2. Assign carry the value 0.

  • Step 3. Let the value T equal that of carry.

  • Step 4. Unless i>j, add mi to the value of T.

  • Step 5. Unless i>k, add ni to the value of T.

  • Step 6. If T=0, stop the entire process.
    If T<10, then let si equal T and let carry equal 0.
    Otherwise, then let si equal (T-10) and let carry equal 1.

  • Step 7. Increment the value of i and go to step 3.


  • Assumed primitives:

  • Can determine whether a number is less than 10.
  • Can add a single digit to a number which is 10 or less.
  • Can subtract 10 from a number, if it is greater than or equal to 10.

  • Michael Goldwasser
    Last modified: Wed Jan 16 23:52:45 CST 2002