liveBook · Manning

Euclid's algorithm

As explained in Modern C

Euclid's algorithm is an algorithm for computing the greatest common divisor of two integers by repeatedly applying remainder operations.

Euclid's algorithm computes the greatest common divisor of two positive integers by repeatedly replacing the larger integer with the remainder of division by the smaller. The gcd remains unchanged through this transformation, and the process ends when the remainder is zero. section 7.3

Recursive gcd computation

For gcd(18, 30), the recursive call begins with gcd2(18, 30), computes 30 % 18, and continues with the remainder and 18 until the zero case returns the gcd.

How Modern C covers it

How deep it goes
Explains the gcd definition, recurrence, preconditions, base case, and recursive C implementation, while deferring detailed assert treatment.
Examples
Uses gcd2(18, 30), a recursive call diagram, and short C listings as concrete demonstrations.
What it assumes
Assumes integers, divisibility, modulus, and function calls while teaching the gcd recurrence and recursive function model.
What you can do after
Build it from scratch as a recursive C function with explicit argument checks, a zero base case, and remainder-based recursion.
How it connects
Connects the algorithm to recursion, function calls, modulus, preconditions, and assertions.

Also explained in

Compare how 3 books explain Euclid's algorithm →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Modern C. The section links point to the parts of the book it draws on.