Professional Documents
Culture Documents
1
Arithmetic with tally marks
Adding two numbers in tally marks just means concatenating the marks:
kk + kkkkk = kkkkkkk
Multiplying two numbers in tally marks means expanding each tally in one to
the number of tally marks in the other:
kk × kkkkk = kkkkk kkkkk
So basic arithmetic involving tally marks can be easily given as a step by step
process.
The main point to note is that the amount of time required to do the “compu-
tation” is equal to the length of the answer! The problem is that the length of
the answer in this case is the same as the size of the number, which can be very
large!
Decimal notation allows us to reduce the length of the answer. As we shall see
this also allows us to reduce the time required to do the computation.
Decimal Arithmetic
The key ideas in carrying out decimal arithmetic are addition and multiplication
tables, and the carry forward mechanism.
The following arguments are indicative rather than formal. They are meant to
show that one can estimate the number of steps required to carry out decimal
arithmetic in terms of the length of the numbers being operated on.
Addition of decimals
Faced with the question of computing d + e (respectively d · e) where d and e
are digits, one performs a “table lookup” operation to get is value in the form
cb where c and b are digits; here c is called the carry over and is at most 1 for
addition (respectively, at most 8 for multiplication).
Given the question of adding the decimal numbers dn dn−1 . . . d0 and en en−1 . . . e0 ,
we see (by induction) that it is of the form cn+1 bn bn−1 . . . b0 where bi are digits
and cn+1 is a carry over digit which is 0 or 1.
The addition table lookup gives us such a method for n = 0. For n > 1 we reduce
the problem as follows. Suppose we have already carried out the addition of
dn−1 . . . d0 and en−1 . . . e0 to get cn bn−1 . . . d0 . Note that dn + en ≤ 9 + 9 = 18
so that, (dn + en ) + cn ≤ 18 + 1 ≤ 19. This shows that dn + en + cn = cn+1 bn
where cn+1 is 0 or 1. The answer we want is cn+1 bn bn−1 . . . b0 .
We see that if A(n) steps are required to calculate the sum of numbers of length
n, then A(0) consists of a single table lookup which takes (say) C steps. The
above shows that A(n + 1) ≤ A(n) + 2C since the calculation of dn + en + cn
takes 2C steps. It follows that A(n) ≤ 2Cn.
2
Multiplication of decimals
We now look at the question of multiplying the decimal numbers dn dn−1 . . . d0
and en en−1 . . . e0 . The method requires us carry this out in two parts:
• multiply dn dn−1 . . . d0 by ek to get bn+1,k bn,k . . . b0,k , for k running from 0
to n.
• add the numbers bn+1,k bn,k . . . b0,k 0 · · · 0 where there are k trailing zeros,
for k running from 0 to n.
We already know how to carry out the second part and that it will take us at
most 4Cn2 steps.
We carry out the first part as follows, by induction on n. When n = 0 we already
know how to lookup d0 · ek in the table to get b1,k b0,k , a two digit number.
By induction, let us assume that we have already multiplied dn−1 . . . d0 by ek
to calculate cn,k bn−1,k . . . b0,k . To complete the calculation, we now calculate
dn · ek + cn,k to get bn+1,k bn,k as the two digit number. This calculation requires
us to look up the multiplication table once (say D steps) and the addition table
2 times (for the two digits). So this calculation requires D + 2C steps.
So if multiplication of an n digit number by a single digit requires B(n) steps
then we see that B(0) = D and B(n) = B(n − 1) + D + 2C. It follows that
B(n) ≤ (D + 2C)n.
Since we need to do this step for every ek , it follows that the first part requires
B(n)n steps for a number of length n.
We easily conclude that the number of steps required to multiply two n digit
numbers using the standard method of multiplication tables and carry forward
is at most En2 for some constant E.
Other operations
We also need to carry out other operations with decimal numbers:
Comparison: We need to compare dn . . . d0 with en . . . e0 to say which is larger
Subtraction: When dn . . . d0 is greater than en . . . e0 we want to calculate the
difference. (One can also take care of “signs” with a very small amount of
additional effort!)
Division: Given decimal numbers N and M we want to find decimal numbers q
(called dividend) and r (called remainder) which satisfy: 1. N = q · M + r
2. 0 ≤ r < M
To carry these out we will use the standard comparisons:
0<1<2<3<4<5<6<7<8<9
3
One further thing that is required in these calculations is “trial-and-error” for at
most 10 steps.
The trial and error step is in two cases:
dn+1 ≤ en : We are required to find the digit b for which dn+1 dn . . . d0 satisfies
if it exists.
dn ≥ en : We are required to find the digit b for which dn . . . d0 satisfies
if it exists.
This trial and error step is a crucial step in long division. Since b can take only
10 values, one can see that this takes F n steps for some constant F . This can
be used to show that division can also be carried out in Gn2 steps for a suitable
constant G; here n is the length of the numbers input to the long division.
We will not go into the details of these algorithms which can be found in a
number of resources which will be linked in the references.
Polynomial time
In summary, the basic arithmetic operations require at most Cnp steps for some
positive p and some constant C, where n is the length of the input.
Since Cnp < Cnk for where k is an integer larger than p, the number of steps is
bounded by a polynomial in the length n of the input.
Hence, we call such algorithms polynomial time algorithms.
Note that n is the length of the input. As we have seen, the number represent
by an input of size n is roughly of the size 10n .
In particular, any algorithm that works by checking the inputs of length n
one-by-one will take time 10n which is exponential and so enormously larger
than polynomial time.
Even with polynomial time methods available mathematicians are always trying
to find faster ways of multiplying numbers!
Another point worth noting is that mathematically, one is only concerned about
the behaviour of our algorithm large values of n. However, in practical terms a
large constant C could also be a source of long running time for the algorithm.
Worrying about the constants is a job for hardware and software engineers!