MCS-211 · Block 1 Unit 2

Some Pre-requisites and Asymptotic Bounds

Section 2 of 8

Block 1 · Unit 2Some Pre-requisites and Asymptotic Bounds8 min · Section 2 of 8

2.2Logarithms and the Mod Function

Covers IGNOU MCS-211 §2.2.3, 2.2 (Modular Arithmetic/Mod Function)

Logarithms measure “how many times can I halve (or divide by bb) before I reach 1”, which is exactly what binary search and divide-and-conquer do. The mod function gives remainders, used in Euclid’s algorithm, hashing and cyclic counters. This page states the rules correctly and shows a common programming trap with negative remainders.

What a logarithm is

So log⁡28=3\log_2 8 = 3 because 23=82^3 = 8, and log⁡101000=3\log_{10} 1000 = 3. In algorithm analysis, log⁡n\log n without a base usually means log⁡2n\log_2 n, and lg⁡n\lg n is a common short form for log⁡2n\log_2 n.

The rules

For positive a,b,ca, b, c with the bases a,b≠1a, b \ne 1, and any real nn:

RuleFormula
Productlog⁡a(bc)=log⁡ab+log⁡ac\log_a(bc) = \log_a b + \log_a c
Powerlog⁡a(bn)=nlog⁡ab\log_a(b^n) = n \log_a b
Reciprocallog⁡a(1/b)=−log⁡ab\log_a(1/b) = -\log_a b
Swap base and argumentlog⁡ab=1log⁡ba\log_a b = \dfrac{1}{\log_b a}
Change of baselog⁡ba=log⁡calog⁡cb\log_b a = \dfrac{\log_c a}{\log_c b} for any valid base cc
Exponent swapalog⁡bc=clog⁡baa^{\log_b c} = c^{\log_b a}
Python · log_rules.py
import math

a, b, c, n = 2, 8, 32, 3
log = math.log    # log(x, base)

print("log_2(8*32)  =", round(log(b * c, a), 6), " log_2 8 + log_2 32 =", round(log(b, a) + log(c, a), 6))
print("log_2(8^3)   =", round(log(b ** n, a), 6), " 3*log_2 8          =", round(n * log(b, a), 6))
print("log_8 2      =", round(log(a, b), 6), " 1/log_2 8          =", round(1 / log(b, a), 6))
print("log_2(1/8)   =", round(log(1 / b, a), 6), " -log_2 8           =", round(-log(b, a), 6))
print("log_8 32     =", round(log(c, b), 6), " log_2 32/log_2 8   =", round(log(c, a) / log(b, a), 6))
print("2^(log_8 32) =", round(a ** log(c, b), 6), " 32^(log_8 2)       =", round(c ** log(a, b), 6))
print("printed rule (iii) log_8 2 = log_2 8 ?", math.isclose(log(a, b), log(b, a)))
print("printed rule (iv) log_2(1/8) = -log_8 2 ?", math.isclose(log(1 / b, a), -log(a, b)))
Output
log_2(8*32)  = 8.0  log_2 8 + log_2 32 = 8.0
log_2(8^3)   = 9.0  3*log_2 8          = 9.0
log_8 2      = 0.333333  1/log_2 8          = 0.333333
log_2(1/8)   = -3.0  -log_2 8           = -3.0
log_8 32     = 1.666667  log_2 32/log_2 8   = 1.666667
2^(log_8 32) = 3.174802  32^(log_8 2)       = 3.174802
printed rule (iii) log_8 2 = log_2 8 ? False
printed rule (iv) log_2(1/8) = -log_8 2 ? False

Why the base does not matter inside O

By change of base, log⁡10n=log⁡2nlog⁡210≈0.301log⁡2n\log_{10} n = \dfrac{\log_2 n}{\log_2 10} \approx 0.301 \log_2 n. Logarithms to different bases differ only by a constant factor, and big-O ignores constant factors. So O(log⁡2n)=O(log⁡10n)=O(ln⁡n)O(\log_2 n) = O(\log_{10} n) = O(\ln n), and we simply write O(log⁡n)O(\log n).

The second result matters in Unit 4: recurrence solutions often look like alog⁡bna^{\log_b n}, and the exponent swap turns them into a power of nn.

The mod function

  • 42 mod 11=942 \bmod 11 = 9, because 42=3×11+942 = 3 \times 11 + 9.
  • −42 mod 11=2-42 \bmod 11 = 2, because −42=(−4)×11+2-42 = (-4) \times 11 + 2 and 0≤2<110 \le 2 < 11.

The mathematical mod is never negative. Programming languages do not all agree: in C (since C99) the % operator takes the sign of the dividend, so -42 % 11 is -9, while Python’s % follows the mathematical definition and gives 2.

C · mod_demo.c
#include <stdio.h>

/* Mathematical b mod n for n > 0: always gives 0 <= r < n. */
int math_mod(int b, int n)
{
    int r = b % n;          /* C's %: sign follows b (C99 and later) */
    return (r < 0) ? r + n : r;
}

int main(void)
{
    int values[] = {42, -42, 11, -11, 7};
    int n = 11;
    for (int i = 0; i < 5; i++) {
        int b = values[i];
        printf("b = %3d: b / n = %2d, C b %% n = %3d, b mod n = %2d\n",
               b, b / n, b % n, math_mod(b, n));
    }
    return 0;
}
Output
b =  42: b / n =  3, C b % n =   9, b mod n =  9
b = -42: b / n = -3, C b % n =  -9, b mod n =  2
b =  11: b / n =  1, C b % n =   0, b mod n =  0
b = -11: b / n = -1, C b % n =   0, b mod n =  0
b =   7: b / n =  0, C b % n =   7, b mod n =  7

C’s integer division truncates towards zero (−42/11=−3-42 / 11 = -3), so its remainder must be −42−(−3)(11)=−9-42 - (-3)(11) = -9. The mathematical mod uses the floor instead: k=⌊−42/11⌋=−4k = \lfloor -42/11 \rfloor = -4, giving remainder 2.

Key takeaways

  • log⁡bx=y\log_b x = y means by=xb^y = x; bases must be positive and not 1.
  • log⁡a(1/b)=−log⁡ab\log_a(1/b) = -\log_a b and log⁡ba=1/log⁡ab\log_b a = 1/\log_a b; the study material’s (iii) and (iv) are misprinted.
  • Change of base shows all log bases differ by constants, so O(log⁡n)O(\log n) has no base.
  • b mod n∈{0,…,n−1}b \bmod n \in \{0, \dots, n-1\}; C’s % can be negative, Python’s % cannot (for n>0n > 0).

Practice questions

Answer on paper first, then check. The explanation matters more than the letter you chose.

Question 1 · multiple choice

What is log⁡82\log_8 2?

Show answer

Answer: B. 13\tfrac13

8^(1/3) = 2, so log₈2 = 1/3. The value 3 is log₂8; the two are reciprocals, not equal.

Question 2 · multiple choice

What is −17 mod 5-17 \bmod 5 (mathematical definition)?

Show answer

Answer: C. 3

−17 = (−4)(5) + 3 with 0 ≤ 3 < 5. The value −2 is what C’s % gives, not the mathematical mod.

Question 3 · multiple choice

What does this program print?

C · quiz_mod.c
#include <stdio.h>

int main(void)
{
    printf("%d %d %d\n", -7 / 2, -7 % 2, 7 % -2);
    return 0;
}
Show answer

Answer: B. -3 -1 1

C division truncates towards zero, so −7/2 = −3, and the remainder takes the dividend’s sign: −7 % 2 = −1, 7 % −2 = 1. The floor-based answer −4 is what Python’s // would give.

Question 4 · multiple choice

Simplify 2log⁡2n2^{\log_2 n}.

Show answer

Answer: D. nn

By the definition of the logarithm, raising the base to log_b n gives n back.

Question 5 · multiple choice

Why is O(log⁡10n)O(\log_{10} n) the same as O(log⁡2n)O(\log_2 n)?

Show answer

Answer: B. Because log⁡10n=log⁡2n/log⁡210\log_{10} n = \log_2 n / \log_2 10, a constant multiple.

Change of base shows the two differ by the constant 1/log₂10 ≈ 0.301, and big-O ignores constant factors. The two logs are not equal.

Question 6 · short answer

Using the log rules, show that log⁡2(n3⋅8)=3log⁡2n+3\log_2(n^3 \cdot 8) = 3\log_2 n + 3, and find its value for n=4n = 4.

Show worked answer

Product rule: log₂(n³ · 8) = log₂ n³ + log₂ 8. Power rule: log₂ n³ = 3 log₂ n, and log₂ 8 = 3. So the expression is 3 log₂ n + 3. For n = 4: 3 × 2 + 3 = 9. Check: 4³ × 8 = 512 = 2⁹.

↑ ↓ move↵ openTab switch type