You are on page 1of 4

Basic Arithmetic Operations

Recall that encryption and decryption were defined as operations (permutations)


on a set M = Al where A is the underlying alphabet of letters and punctuation
marks.
As we saw earlier, the amount of time required to carry out these operations
can determine the strength or weakness of encryption mechanisms.
Mathematically we can replace A by another set of the same size. Hence, it is
convenient to “encode” the elements of A as the “base” of a number system.
Thus, there is a “standard” system of encoding language as numbers. On modern
computers, we use the Unicode standard which assigns one number between 0
and 65535 for each symbol used in any script for any language that is currently
active! Using this, we can convert all messages into numbers.
In fact, everything stored on a computer is stored in the form of numbers at the
“lowest” level.
So, it becomes important to see how much time it takes to carry out basic
arithmetic operations on numbers.

Tally marks to decimals


Originally numbers were denoted by “tally marks”.
k, kk, kkk, kkkk, . . .
However, as one deals with bigger and bigger number this is time consuming
and difficult to keep track of.
Following the same pattern that we used for language, we introduce different
symbols for different numbers. However, numbers are infinite, so we cannot keep
inventing new symbols!
So we invented the “decimal system” which is a “place system”. This works with
a fixed set of “digits”
{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}
The meaning of the digit is different depending upon where it is placed. In the
first place on the right it means a number from 0 to 9; in the second place, it
means 10 times as much; in the third place 100 times as much; and so on.
In other words, the number involving N tally marks is represented by a decimal
number dn dn−1 . . . d0 where
N = d0 + 10 · d1 + · · · + 10n · dn
So we can represent numbers between 0 and 10n − 1 in strings of length n. In
other words, the decimal system us to represent big numbers by (comparatively)
short strings.
Does this improvement also extend to arithmetic operations?

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

We will also use the addition and multiplication tables.

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

(b + 1) · (en . . . e0 ) > dn+1 dn . . . d0 ≥ b · (en . . . e0 )

if it exists.
dn ≥ en : We are required to find the digit b for which dn . . . d0 satisfies

(b + 1) · (en . . . e0 ) > dn . . . d0 ≥ b · (en . . . e0 )

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!

You might also like