Earlier lessons count each small act, such as one comparison, as one step. Is adding two numbers of 300 digits one step too?
1a worked example
A computer keeps numbers in words, numbered boxes of memory, each made of 64 bits on most of today's computers.
A bit is a digit that is 0 or 1. 3 bits make 8 patterns, 000 to 111, and in order they name the numbers 0 to 7, as the table shows.
The table's 3 bits give patterns, and 64 bits give 64 twos multiplied, the numbers from 0 to , about 18 billion billion, a number of 20 digits.
Each of these is one step. Read a word, which brings its number out of memory to be worked on, or write a number into a word. Add, take away, multiply, divide or take the remainder of two numbers held in words. Compare two of them.
To write the larger of places 5 and 9 into place 0, read place 5, read place 9, compare, and write. That is 4 steps.
3 bits
the number
000
0
001
1
010
2
011
3
100
4
101
5
110
6
111
7
3 bits write 8 numbers, 0 to 7. Each more bit doubles the count.
2how it works
Memory is counted in words too. A list of numbers, each small enough for one word, takes words.
bits in the number
words of 64 bits
64
1
65
2
128
2
200
4
1000
16
One more bit past a word's 64 needs a whole second word.
A number of bits takes words, rounded up, as a part-filled word is still a whole word. For 200 bits, , so words.
10 bits write numbers (lesson 3.5), 0 to 1023, enough for any 3 digits. 300 digits are 100 groups of 3, so they need about bits, which fill 16 words.
Adding two such numbers goes word by word, carrying as on paper, so it takes about 16 times the steps of one word. So adding two 300-digit numbers is not one step.
3in symbols
A job's extra space is the words it uses beyond its input, the words holding what it is given. Adding up a list of small numbers into one total uses 2 extra words, the total and the counter, whatever is.
Making a reversed copy of the list uses extra words, the copy and a counter.
This way of counting is called the word-RAM model. RAM is short for random-access machine, a computer whose memory places can be read in any order.
It counts one step for each act on a word and one place for each word. Pat Morin's free textbook Open Data Structures describes it in section 1.4, with words of bits, and this page takes .
job on a list of numbers
input words
extra words
add them into one total
2
find the largest
2
make a reversed copy
Only the copy's extra words grow with .
mind these
Counting steps measures how long a job takes. Counting words measures how much memory it uses.
4your turn
Work each one out. Where there is a box, type your answer and check it. Each exercise's worked answer opens under it.
1exercise 1
A job swaps the numbers in places 2 and 6 by three copies: place 2 into place 0, place 6 into place 2, then place 0 into place 6. Each copy reads one place and writes another.
worked answer
Copy place 2 into place 0, a read and a write, 2 steps.
Copy place 6 into place 2, 2 more.
Copy place 0 into place 6, 2 more, so 6 in all.
Check it another way: 3 copies, each a read and a write, make 6 steps.
2exercise 2
A number has 150 bits. Each word holds 64 bits.
worked answer
is about 2.34.
2 words hold only 128 bits, so a third word is needed, .
Check it another way: 3 words hold 192 bits, enough for 150, and 2 hold 128, too few.
3exercise 3
A list holds 500 numbers, each of 100 bits, in words of 64 bits. Each number starts in a word of its own.
worked answer
Each number needs words.
500 numbers take words.
Check it another way: 1000 words hold 64000 bits, and 500 numbers of 100 bits fill 50000 of them, with each number's spare bits unused.
4exercise 4
Three jobs each take a list of small numbers.
worked answer
Adding into a total keeps a total and a counter, 2 words.
Finding the largest keeps the largest so far and a counter, 2 words.
A reversed copy needs a new place for each of the numbers and a counter, words.
Check it another way: Only the copy's extra words change when changes.
5exercise 5
Adding two big numbers word by word takes 4 steps for each word of one number: 2 reads, one word of each, then an add and a write. Each number has 512 bits, in words of 64 bits.
worked answer
Each number fills words.
At 4 steps for each of the 8 words, steps.
Check it another way: Twice the bits, 1024, would be 16 words and 64 steps, so the steps grow with the length of the numbers.