Introduction to Problem Solving
Algorithms
Write an algorithm and check its characteristics.
An algorithm is a finite sequence of steps that gives the desired output in a finite amount of time. It has a definite beginning and a definite end. The word comes from the name of the Persian mathematician Al-Khwarizmi.
An example: find the GCD (greatest common divisor) of 24 and 36.
- Step 1: list the divisors of 24: 1, 2, 3, 4, 6, 8, 12, 24.
- Step 2: list the divisors of 36: 1, 2, 3, 4, 6, 9, 12, 18, 36.
- Step 3: find the divisors common to both: 1, 2, 3, 4, 6, 12.
- Step 4: the largest of these is the GCD: 12.
An algorithm is the roadmap of a program, its building block. Search engines, messaging apps, taxi booking apps and banking systems all run on algorithms. A clear algorithm makes a solution reliable, accurate and efficient.
A good algorithm has these characteristics:
- Precision: every step is clearly stated.
- Uniqueness: the result of each step depends only on the input and the earlier steps.
- Finiteness: it always stops after a finite number of steps.
- Input: it receives some input.
- Output: it produces some output.
While writing an algorithm, identify three things: the input, the processing to be done, and the output expected.
Practise this lesson
Questions that get harder as you improve, a Tutor that helps without giving the answer away, and progress you can see. Free for every learner.
No account, no email address. Opens the real product.