Solving Problems Step by Step
An algorithm is a finite, precise sequence of unambiguous steps that solves a problem or computes a result. The word predates computers — Euclid's method for the gcd (Unit 2) is an algorithm — but programming is the art of expressing algorithms so a machine can carry them out. For a procedure to be a genuine algorithm it should be finite (it terminates), definite (each step is unambiguous), and effective (each step is actually doable).
Computational thinking is the problem-solving mindset behind algorithm design. Its core habits:
- Decomposition — break a big problem into smaller, manageable subproblems.
- Pattern recognition — spot repeated structure that can be handled uniformly.
- Abstraction — focus on the essential features, hiding irrelevant detail.
- Algorithm design — express the solution as an ordered sequence of steps.
Before coding, an algorithm is often written as pseudocode — structured, language-independent English that captures the logic without the syntax of any particular language — or as a flowchart. This lets you reason about correctness and efficiency separately from implementation details.
A subtle but essential point: a mathematical definition is not automatically an algorithm. "The gcd is the largest common divisor" defines the gcd but does not say how to compute it; Euclid's algorithm does. An algorithm must give a concrete, terminating procedure, not just characterise the answer. This distinction — between what a thing is and how to compute it — is the heart of turning mathematics into programs.
Common pitfall: confusing a definition or a specification (what the answer is) with an algorithm (a step-by-step procedure for computing it). "The prime factorisation is the unique product of primes" specifies the answer but is not an algorithm; you still need a concrete, terminating procedure (trial division, say) to actually produce it. An algorithm must tell you how, not merely what.