Intro to Algorithms, 2019-20
Tutorial 1 – Notes

The integers

We will begin our study of algorithms by looking at algorithms that work with integers. We distinguish

Z = { ..., -2, -1, 0, 1, 2, ... } : the set of all integers

N = { 0, 1, 2, ... } : the set of natural numbers

{ 1, 2, ... } : the positive integers

The mathematical study of the integers is called number theory.

Integer division

A basic result of elementary number theory is the division theorem:

For any integer a and any integer b > 0, there exist unique integers q and r, with 0 ≤ r < b, such that

a = q · b + r

Given a and b, the functions div (integer division) and mod (integer remainder) compute the values q and r. We write

So for all integers a and b with b > 0,

a = (a div b) · b + (a mod b)

For example:

In Python, the // operator performs integer division, and the % operator computes the remainder. So

Note that if a is evenly divisible by b, then the remainder after dividing is 0. So we can test whether b divides a by checking the remainder. In Python, for example:

n = int(input('Enter n: '))
if n % 7 == 0:
  print(n, 'is divisible by 7')
else:
  print(n, 'is not divisible by 7')

Hours, minutes, and seconds

There are 60 · 60 · 24 = 86,400 seconds in a day. Suppose that we are given an integer 0 ≤ t < 86,400 representing a number of seconds since midnight. We'd like to compute the number of hours, minutes, and seconds since midnight, yielding a time such as 3:24:54.

In other words, we want to find integers 0 ≤ h < 24, 0 ≤ m < 60 and 0 ≤ s < 60 such that

t = 3600 h + 60 m + s

How can we do this? One approach is to first compute the number of hours: h = t // 3600. Then t % 3600 is the number of seconds that remain after subtracting the h hours. This yields a Python implementation like this:

t = int(input('How many seconds? '))

hours = t // 3600
t = t % 3600
mins = t // 60
secs = t % 60

print(hours, ':', mins, ':', secs)

As another possible approach, we can first compute the number of seconds: s = t % 60. Then t // 60 is the number of minutes that remain after subtracting the s seconds. In Python:

t = int(input('How many seconds? '))

secs = t % 60
t = t // 60  # now t is the total number of minutes
mins = t % 60
hours = t // 60

print(hours, ':', mins, ':', secs)

Decimal representation

By convention, we use decimal representation (base 10) when writing numbers in everyday life. When we write a number such as 1985, we actually mean a sum of powers of 10:

1985 = 1 · 103 + 9 · 102 + 8 · 10 + 5

We will now discuss simple algorithms that can split an integer (e.g. 1985) into a series of decimal digits (1, 9, 8, 5), and can combine a series of decimal digits into an integer.

Splitting into digits

To split into digits, we first notice that for any integer i,

We can see this mathematically by factoring the expansion of 1985 above:

1985 = 1 · 103 + 9 · 102 + 8 · 10 + 5 = 10 (1 · 102 + 9 · 10 + 8) + 5

From this factoring it is evident that 1985 % 10 = 5 and 1985 // 10 = (1 · 102 + 9 · 10 + 8) = 198.

So we can write a simple loop that extracts digits from an integer in succession. In Python:

n = int(input('Enter n: '))

while n > 0:
  d = n % 10
  n = n // 10
  print('digit: ', d)

By the way, note the similarity of this problem to the previous exercise involving times. The time 3:24:54 is 12,294 seconds past midnight, since

12,294 = 3 · 602 + 24 · 60 + 54

So a time such as 3:24:54 is actually like a number written in base 60. (If this seems unclear, don't worry – we will soon discuss bases other than base 10.)

Combining digits

To combine a series of digits into a number, we first notice that we can append a decimal digit d to an integer n using this formula:

n' = 10 n + d

For example, if n = 198 and d = 5, then n' = 10 · 198 + 5 = 1985.

So we can write a simple loop that reads a series of digits and combines them into an integer. In Python:

n = 0
while True:
  d = int(input('Digit: '))
  if d < 0:
    break
  n = 10 * n + d
print('n is', n)

The program behaves like this:

$ py joinDigits.py
Digit: 1
Digit: 9
Digit: 8
Digit: 5
Digit: -1
n is 1985

$

As some students pointed out, we can rewrite the preceding loop without break (though at the cost of performing one unnecessary multiplication operation):

n = 0
d = 0
while d >= 0:
  n = 10 * n + d
  d = int(input('Digit: '))
print('n is', n)