MCS-211 · Block 1 Unit 1

Basics of an Algorithm and its Properties

Section 2 of 9

Block 1 · Unit 1Basics of an Algorithm and its Properties7 min · Section 2 of 9

1.2Euclid's Algorithm: A First Example

Covers IGNOU MCS-211 §1.2

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\gcd(12, 18) = 6 and gcd⁡(7,0)=7\gcd(7, 0) = 7 (every integer divides 0).

The idea behind Euclid’s method

Here a mod ba \bmod b is the remainder when aa is divided by bb. Because 0≤a mod b<b0 \le a \bmod b < 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 ← r
end while
return a

When the loop ends, b=0b = 0, and by Euclid’s rule gcd⁡(a,0)=a\gcd(a, 0) = a. So the algorithm must return aa.

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: 0
return 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 =  147
a =  462, b =  147, r = a mod b =   21
a =  147, b =   21, r = a mod b =    0
gcd(1071, 462) = 21

Checking the five properties

PropertyHow Euclid’s algorithm satisfies it
InputTwo non-negative integers, not both zero.
OutputOne integer, gcd⁡(a,b)\gcd(a, b).
DefinitenessEach step (remainder, two assignments) has one meaning.
EffectivenessDivision with remainder and assignment are basic operations.
Finitenessbb strictly decreases and stays ≥0\ge 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,a mod b)\gcd(a, b) = \gcd(b, a \bmod b) and gcd⁡(a,0)=a\gcd(a, 0) = a.
  • The loop runs while b≠0b \ne 0 and the answer is the final value of aa.
  • 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 1 · multiple choice

What is the value returned by the correct GCD-Euclid algorithm when the loop ends?

Show answer

Answer: C. The final value of a

The loop stops when b becomes 0, and gcd(a, 0) = a. Returning b would always give 0.

Question 2 · multiple choice

What is gcd⁡(48,18)\gcd(48, 18), and how many rounds does Euclid’s algorithm take?

Show answer

Answer: B. 6, in 3 rounds

48 mod 18 = 12 → (18, 12); 18 mod 12 = 6 → (12, 6); 12 mod 6 = 0 → (6, 0). Three rounds, answer 6. Stopping after two rounds would wrongly suggest 12 is involved in the answer.

Question 3 · multiple choice

What does this program print?

C · quiz_gcd_rounds.c
#include <stdio.h>

int main(void)
{
    unsigned int a = 100, b = 75, rounds = 0;
    while (b != 0) {
        unsigned int r = a % b;
        a = b;
        b = r;
        rounds++;
    }
    printf("%u %u\n", a, rounds);
    return 0;
}
Show answer

Answer: D. 25 2

100 mod 75 = 25 → (75, 25); 75 mod 25 = 0 → (25, 0). Two rounds, a = 25. Three rounds would need one more non-zero remainder.

Question 4 · multiple choice

Why does Euclid’s algorithm always stop?

Show answer

Answer: B. Because b strictly decreases and can never go below 0

The new b is a mod b, which is smaller than the old b and at least 0, so b must reach 0. a and b do not need to become equal.

Question 5 · multiple choice

What is gcd⁡(0,9)\gcd(0, 9)?

Show answer

Answer: C. 9

Round 1: 0 mod 9 = 0, so the pair becomes (9, 0) and the answer is 9. Only gcd(0, 0) is undefined.

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.

↑ ↓ move↵ openTab switch type