Basic Log Facts

CS 244 Data Structures and Algorithms
Fall 1998 Moravian College
Tom Linton,
http://www.cs.moravian.edu/~linton/

NAMES:

While the natural logarithm, [Maple Math] , is by far the most convenient logarithm for calculus, in computer science the base 2 logarithm, [Maple Math] , is nearly always used. This is due to the binary nature of computers and the frequency with which problems are solved by "dividing them in half", and solving the 2 smaller pieces. All logarithms (meaning [Maple Math] for any [Maple Math] > 1) are multiples of one another, so if you understand one logarithm function, you know them all. Take a look at a plot of several quotients of different logarithmic functions. NOTE: in Maple, both [Maple Math] and [Maple Math] represent the natural log.

> plot([ln(n)/log[2](n), log[5](n)/log[10](n),
log[8](n)/log[2](n)],
n=1..10,y=0..1.5, title="log ratios",
color=[red,black,blue]);

>

[Maple Plot]

The fact that all those ratios are constant means the logarithms are multiples. For example, the red line is a plot of [Maple Math] and because the graph is a horizontal line y = c (where c is close to 0.7), we see that

[Maple Math] , or [Maple Math] .

The value of c, in this case is ln(2).

> ln(2.);

[Maple Math]

Recall that [Maple Math] , means that [Maple Math] (a log is asking "what power of b gives n "). This explains why all logs are just multiples of one another. Say [Maple Math] . That's the same as saying that [Maple Math] . Now, if we take the natural log of both sides of this equation we get [Maple Math] . Using a property of logs, namely that [Maple Math] , we arrive at

[Maple Math] or [Maple Math] .

Finally, we started out by saying that [Maple Math] , so we now have [Maple Math] . NOTE: you can compute logs to other bases on your calculator with this, since a similar derivation will show that

[Maple Math] ,

and nearly all calculators today have a natural log key. For example, [Maple Math] .

>

Logs arise frequently in the analysis of the run time of algorithms. Repeatedly dividing (integer division is best here) n by a fixed number b is related to [Maple Math] . Let's go with n = 107 and b = 5. Just keep dividing by 5 until the result is zero, and count the number of divisions.

> div:=proc(n::integer, b::posint)
local val, count, ans;
val:=n:
count:=0:
ans:=[val]:
while val > 0 do
val:= floor(val/b);
ans:=[op(ans),val];
count:=count+1;
od;
print(divides = ans, num_divides = count);
end:

> div(107,5);

[Maple Math]

> log[5](107.);

[Maple Math]

> div(534,3);

[Maple Math]

> log[3](534.);

[Maple Math]

> div(12345678,2);

[Maple Math]
[Maple Math]

> log[2](12345678.);

[Maple Math]

>

Take n = your student ID number, and select b = 2, 3, 6, or 8. Repeatedly (integer) divide n by b until the result is zero. How many divisions did you perform?

Now, what is [Maple Math] ?


Make up a statement (about
[Maple Math] and dividing repeatedly) that summarizes what you're seeing above.

Return to CS 244 materials page.