The study material introduces algorithms through one of the oldest known algorithms: Euclid’s method for the greatest common divisor. It is short, fast and a perfect example of all five properties.
The problem
For example, gcd(12,18)=6 and gcd(7,0)=7 (every integer divides 0).
The idea behind Euclid’s method
Here amodb is the remainder when a is divided by b. Because 0≤amodb<b, the second number gets strictly smaller every round, so the method must stop.
Pseudocode
GCD-Euclid pseudocode
Algorithm GCD-Euclid(a, b)// Input : non-negative integers a and b, not both zero// Output: gcd(a, b)while b ≠ 0 do r ← a mod b a ← b b ← rend whilereturn a
When the loop ends, b=0, and by Euclid’s rule gcd(a,0)=a. So the algorithm must return a.
C · gcd_return_check.c
#include <stdio.h>/* Same loop, two different return statements. */unsigned int gcd_returns_b(unsigned int a, unsigned int b){ while (b != 0) { unsigned int r = a % b; a = b; b = r; } return b; /* as printed in the study material */}unsigned int gcd_returns_a(unsigned int a, unsigned int b){ while (b != 0) { unsigned int r = a % b; a = b; b = r; } return a; /* correct */}int main(void){ printf("return b: %u\n", gcd_returns_b(1071, 462)); printf("return a: %u\n", gcd_returns_a(1071, 462)); return 0;}
Output
return b: 0return a: 21
A complete C program with a trace
C · gcd_euclid.c
#include <stdio.h>/* Euclid's algorithm with a printed trace. Input: two non-negative integers, not both zero. */unsigned int gcd_euclid(unsigned int a, unsigned int b){ while (b != 0) { unsigned int r = a % b; /* remainder */ printf("a = %4u, b = %4u, r = a mod b = %4u\n", a, b, r); a = b; /* old divisor becomes dividend */ b = r; /* remainder becomes divisor */ } return a; /* b is 0 here, so the answer is a */}int main(void){ unsigned int g = gcd_euclid(1071, 462); printf("gcd(1071, 462) = %u\n", g); return 0;}
Output
a = 1071, b = 462, r = a mod b = 147a = 462, b = 147, r = a mod b = 21a = 147, b = 21, r = a mod b = 0gcd(1071, 462) = 21
Checking the five properties
Property
How Euclid’s algorithm satisfies it
Input
Two non-negative integers, not both zero.
Output
One integer, gcd(a,b).
Definiteness
Each step (remainder, two assignments) has one meaning.
Effectiveness
Division with remainder and assignment are basic operations.
Finiteness
b strictly decreases and stays ≥0, so the loop ends.
Euclid’s algorithm is also very fast: the number of rounds grows only like the logarithm of the smaller number (you will analyse this in Unit 3).
Key takeaways
gcd(a,b)=gcd(b,amodb) and gcd(a,0)=a.
The loop runs while b=0 and the answer is the final value of a.
The second number strictly decreases, which proves finiteness.
Euclid’s algorithm satisfies all five properties and is very efficient.
Practice questions
Answer on paper first, then check. The explanation matters more than the letter you chose.
Question 6 · short answer
Trace Euclid’s algorithm for gcd(252, 105) in a table.
Show worked answer
Round 1: a = 252, b = 105, r = 252 mod 105 = 42 → (105, 42). Round 2: a = 105, b = 42, r = 21 → (42, 21). Round 3: a = 42, b = 21, r = 0 → (21, 0). b = 0, so the algorithm returns a = 21. Check: 252 = 21 × 12 and 105 = 21 × 5.
MCA Online notes are made for reading on screen. Open https://mcastudy.online/mcs-211/basics-of-an-algorithm-and-its-properties/euclid-s-algorithm-a-first-example/ to read this page.