Back to Blog

Basic Concepts of Computer Programming

"Practice yourself, for heaven's sake, in little things; and thence proceed to greater." , Epictetus, Discourses IV.i (c. A.D. 110)

The notion of an algorithm is basic to all of computer programming. Before we write a single line of code, we need to understand what an algorithm actually is, and what distinguishes a rigorous algorithm from an informal set of instructions.

This post explores the foundational concepts that every programmer should know, drawn from the timeless principles in Donald Knuth's The Art of Computer Programming.

What is an Algorithm?

The word "algorithm" has a fascinating history. It comes from the name of a Persian mathematician, al-Khwārizmī (c. 825 CE), who wrote influential texts on arithmetic and algebra. The word evolved through centuries of use, eventually settling into its modern form.

But what is an algorithm? It's similar to a recipe, process, method, or procedure, but with crucial differences.

What Makes an Algorithm an Algorithm? Click each property to see examples and counter-examples
RecipeInformal Procedure
1. Add a dash of salt
2. Stir until it looks right
3. Cook until golden brown
4. Season to taste
Vague, subjective, may not always work
AlgorithmEuclid's Algorithm
E1. Divide m by n, let r = remainder
E2. If r = 0, output n and stop
E3. Set m ← n, n ← r, go to E1
Precise, deterministic, always terminates
The 5 Essential Properties

An algorithm must satisfy five essential properties:

  1. Finiteness: It must terminate after a finite number of steps
  2. Definiteness: Each step must be precisely defined
  3. Input: Zero or more inputs from specified sets
  4. Output: One or more outputs related to the inputs
  5. Effectiveness: Operations must be basic enough to do exactly

These properties distinguish algorithms from vague instructions like "cook until done" or infinite loops that never terminate.

The Five Properties of Algorithms Each property is essential - violate any one and it's not an algorithm
Finiteness

An algorithm must always terminate after a finite number of steps.

In Algorithm EAfter step E1, r < n. If r ≠ 0, n decreases at E3. A decreasing sequence of positive integers must terminate.
Violation ExampleA procedure that says "keep trying until you succeed" with no guaranteed end.
NoteA procedure without finiteness is called a computational method, not an algorithm.

The Assignment Operation

Before we look at our first algorithm, we need to understand a fundamental operation: assignment.

In algorithms, we use the arrow notation for assignment. When we write:

n ← n + 1

This means "replace the value of n with n plus 1." The arrow distinguishes assignment from equality testing. We don't say "n = n + 1" (which would be mathematically false), we say "n gets n + 1."

Crucially, the order of assignments matters. When executing multiple assignments, the sequence can completely change the result.

The Assignment Operation: Order Matters See why 'm ← n, n ← r' is different from 'n ← r, m ← n'
119
m
68
n
51
r
n ← r, m ← n

Initial state

This is why Euclid's algorithm uses m ← n, n ← r in that specific order, reversing it would lose the original value of n before we could save it.

Euclid's Algorithm

Now we're ready for our first complete algorithm: Euclid's algorithm for finding the greatest common divisor (GCD) of two positive integers.

This algorithm is over 2,300 years old, appearing in Euclid's Elements (Book 7). Despite its age, it remains one of the most efficient methods for computing GCDs.

Algorithm E (Euclid's algorithm). Given two positive integers m and n, find their greatest common divisor.

  • E1. [Find remainder.] Divide m by n and let r be the remainder.
  • E2. [Is it zero?] If r = 0, the algorithm terminates; n is the answer.
  • E3. [Reduce.] Set m ← n, n ← r, and go back to step E1.

Let's trace through this algorithm step by step:

Euclid's Algorithm (Algorithm E) Find the greatest common divisor of two positive integers

Try different values to see how the algorithm works. Notice that it always terminates, this is the finiteness property in action.

Quick GCD Calculator
gcd(,) =6(3 steps)

Why Does Euclid's Algorithm Work?

The key insight is a mathematical property: gcd(m, n) = gcd(n, r).

When we divide m by n to get remainder r, the common divisors of m and n are exactly the same as the common divisors of n and r. This means we can keep reducing the problem until r becomes 0.

Why Euclid's Algorithm Works The key insight: gcd(m,n) = gcd(n,r)
The Division

When we divide m by n, we get: m = qn + r

m=q×n+r

Since r is always less than n, and n becomes r in each iteration, the values are strictly decreasing. A decreasing sequence of positive integers must eventually reach zero, guaranteeing termination.

Extended Euclid's Algorithm

There's a more powerful version of Euclid's algorithm that not only finds the GCD, but also finds integers a and b such that:

am + bn = gcd(m, n)

This is incredibly useful in cryptography (for computing modular inverses) and number theory.

Extended Euclid's Algorithm Find a and b such that am + bn = gcd(m, n)

The algorithm maintains additional variables a, a', b, b' that track the linear combinations as we go. At the end, we can verify that the coefficients actually work.

Mathematical Induction

How do we prove that an algorithm always works? How do we prove mathematical statements about all positive integers?

The answer is mathematical induction, a proof technique that mirrors the structure of algorithms themselves.

Proof by Mathematical Induction The domino effect: prove P(1), then prove P(k)→P(k+1)
Base Case P(1)
Prove P(1) is true
1 = 1² ✓

We verify the formula works for n=1: the sum of the first 1 odd number is 1, which equals 1².

1
2
3
4
5
...

Induction has two parts:

  1. Base case: Prove P(1) is true
  2. Inductive step: Prove that if P(k) is true, then P(k+1) is also true

Like dominos falling, once P(1) falls, it knocks over P(2), which knocks over P(3), and so on forever.

A Visual Example

Consider the beautiful fact that the sum of the first n odd numbers equals n²:

1 = 1²
1 + 3 = 4 = 2²
1 + 3 + 5 = 9 = 3²
1 + 3 + 5 + 7 = 16 = 4²

We can prove this by induction, but there's also a gorgeous geometric proof:

Sum of Odd Numbers = Perfect Square 1 + 3 + 5 + ... + (2n-1) = n²: a visual proof by induction
1 + 3 + 5 + 7 + 9 = 25 = 5²
The 5×5 Square
L-Shaped Pieces
Layer 1: 1 cell
Layer 2: 3 cells
Layer 3: 5 cells
Layer 4: 7 cells
Layer 5: 9 cells
Induction: Each new odd number (2n+1) forms an L-shape that extends the square from n×n to (n+1)×(n+1). The L has n cells on the bottom, n cells on the right, plus 1 corner = 2n+1 cells.

Each new odd number forms an L-shaped piece that extends the square by one row and one column. The L-shape has (n-1) cells on the bottom, (n-1) cells on the right, plus 1 corner cell = 2n-1 cells.

Analyzing Algorithms

Once we have an algorithm, a natural question is: how efficient is it?

For Euclid's algorithm, we can ask: on average, how many times is step E1 executed?

Let Tn be the average number of times E1 is executed when we know n but let m range over all values from 1 to n.

Analyzing Euclid's Algorithm Average number of steps Tₙ is proportional to ln(n)
n=1n=20
Measured AverageT203.200
Theoretical (≈ 0.84 ln n)2.525
Key insight: The average number of steps grows logarithmically with n. Specifically, Tn ≈ (12 ln 2 / π²) ln n ≈ 0.8427 ln n for large n.

Remarkably, Tn grows logarithmically with n. Specifically:

Tn ≈ (12 ln 2 / π²) ln n ≈ 0.8427 ln n

This means Euclid's algorithm is incredibly efficient, even for very large numbers, it takes only a handful of steps.

What You Now Know

Algorithms have five essential properties. Finiteness, definiteness, input, output, and effectiveness distinguish rigorous algorithms from informal procedures.

Assignment order matters. The sequence m ← n, n ← r is fundamentally different from n ← r, m ← n.

Euclid's algorithm is elegant and efficient. It finds the GCD in O(log n) steps, a result that was proved centuries after Euclid wrote it.

Mathematical induction proves infinite statements. By proving P(1) and P(k) → P(k+1), we prove P(n) for all positive integers.

Algorithm analysis reveals performance. Understanding how efficient an algorithm is matters as much as knowing that it works.

These foundational concepts, algorithms, assignment, termination, correctness, and analysis, form the bedrock upon which all of computer programming is built.