Week 1: Notes

Algorithms

Some introductory notes about algorithms in general:

powers of 2

You should know by heart:

The fact that 210103 allows you to convert between powers of 2 and 10 easily, at least approximately. For example, 226 = 26 · 220 = 64 M ≈ 64,000,000.

exponentiation

You should be familiar with these identities:

logarithms

You should be familiar with these identities:

integers

integers = ℤ
… , -2, -1, 0, +1, +2, +3, ...
natural numbers = non-negative integers = ℕ
= 0, 1, 2, 3, 4, ...

The mathematical study of the integers is called number theory.

divisibility

Let a, d, q ∈ ℤ. If a = dq then a is divisible by d. a is a multiple of d. d is a factor of a.

Example: What are all factors of 6?

1, 2, 3, 6, -1, -2, -3, -6

(Division Theorem) For any a, b ∈ ℤ with b > 0, there exist unique q, r ∈ℤ such that

a = q · b + r and 0 ≤ r < b

We write

q = quotient of a and b, denoted by a div b
r = remainder, denoted by a mod b

For example, if a = 17 and b = 5, then

q = 3
r = 2
17 = 3 · 5 + 2

So 17 div 5 = 3, and 17 mod 5 = 2.

Or if a = -17 and b = 5, then

q = -4
r = 3
-17 = -4 · 5 + 3

So -17 div 5 = -4, and -17 mod 5 = 3.

For any a ∈ ℤ,

a mod 2 = 0 <==> a is even
a mod 2 = 1 <==> a is odd

For any a ∈ ℤ, a div 1 = a, and a mod 1 = 0.

In Python, (a // b) computes a div b. (a % b) computes (a mod b).

Decimal representation

By longstanding convention, basically every society on Earth writes numbers in base 10, otherwise known as decimal. When we write a number such as 1985, we actually mean a sum of powers of 10:

1985 = 1 · 103 + 9 · 102 + 8 · 101 + 50

In the number above, 1 is the most significant digit, i.e. the one that represents the greatest power of 10. 5 is the least significant digit.

numbers in different bases

We use base 10 by convention, but of course the number 10 is arbitrary. We would now like to work with numbers written in non-decimal bases, i.e. bases other than 10. For example, consider base 5. In the base 5 system, every digit has a value from 0 to 4. Consider the number

20315

Here, the subscript 5 means that the number is written in base 5.

The decimal value of the base-5 number above is

2 · 53 + 0 · 52 + 3 · 51 + 10 = 26610

Every positive integer has a unique representation in any base.

Base 2 (binary) is especially common in computer programming. Here are the first natural numbers in base 2:

We also often use base 16 (hexadecimal), in which we have extra digits A = 10, B = 11, C = 12, D = 13, E = 14, F = 15. For example,

FF16 = 25510 because 15 · 161 + 15 · 160 = 240 + 15 = 255