The greatest common divisor
Le plus grand commun diviseur
The greatest common divisor (GCD) of two numbers is the biggest whole number that divides both of them with no remainder.
Le plus grand commun diviseur (PGCD) de deux nombres est le plus grand nombre entier qui les divise tous les deux, sans reste.
The divisors of 12 are 1, 2, 3, 4, 6 and 12; those of 18 are 1, 2, 3, 6, 9 and 18. The biggest one they share is 6, so gcd(12, 18) = 6.
Les diviseurs de 12 sont 1, 2, 3, 4, 6 et 12; ceux de 18 sont 1, 2, 3, 6, 9 et 18. Le plus grand qu’ils ont en commun est 6 : pgcd(12, 18) = 6.
We use the GCD to simplify fractions (12/18 = 2/3), to share things into equal groups, and even in the cryptography that keeps online banking safe.
Le PGCD sert à simplifier les fractions (12/18 = 2/3), à faire des partages en groupes égaux, et même dans la cryptographie qui protège les banques en ligne.
The algorithm
L’algorithme
About 2300 years ago, the Greek mathematician Euclid described a fast way to find the GCD without listing all the divisors. It rests on one idea:
Il y a environ 2300 ans, le mathématicien grec Euclide a décrit une façon rapide de trouver le PGCD sans faire la liste des diviseurs. Elle repose sur une idée :
gcd(a, b) = gcd(b, r)pgcd(a, b) = pgcd(b, r)
where r is the remainder of the division of a by b (in code, a % b).
où r est le reste de la division de a par b (en code, a % b).
- Divide a by b: a = q × b + r, with a quotient q and a remainder r.
- Replace the pair (a, b) with (b, r).
- Start again until the remainder is 0.
- The last divisor, the one that gave a remainder of 0, is the GCD.
- Divise a par b : a = q × b + r, avec un quotient q et un reste r.
- Remplace la paire (a, b) par (b, r).
- Recommence jusqu’à ce que le reste soit 0.
- Le dernier diviseur, celui qui a donné un reste de 0, est le PGCD.
A worked example: gcd(252, 105)
Un exemple : pgcd(252, 105)
Each row passes its divisor and its remainder to the next one: (252, 105) → (105, 42) → (42, 21) → (21, 0).
Chaque ligne passe son diviseur et son reste à la suivante : (252, 105) → (105, 42) → (42, 21) → (21, 0).
The same steps as a picture: cut a 252 × 105 rectangle into the biggest squares you can. 2 squares of 105, then 2 of 42, then 2 of 21. The last squares fill the gap exactly: their side, 21, is the GCD.
Les mêmes étapes en image : découpe un rectangle de 252 × 105 en carrés, les plus grands possible. 2 carrés de 105, puis 2 de 42, puis 2 de 21. Les derniers carrés remplissent juste le reste : leur côté, 21, est le PGCD.
The algorithm in JavaScript
L’algorithme en JavaScript
This is the code that runs the examples and the exercises on this page.
C’est le code qui fait tourner les exemples et les exercices de cette page.
// Returns the GCD of a and b, and every division step.
function euclidSteps(a, b) {
let x = Math.abs(a);
let y = Math.abs(b);
if (x < y) [x, y] = [y, x]; // x is the bigger one
const steps = [];
while (y > 0) {
const q = Math.floor(x / y); // how many times y fits into x
const r = x % y; // the remainder
steps.push({ x, y, q, r });
x = y; // the divisor moves to x
y = r; // the remainder moves to y
}
return { gcd: x, steps }; // x is the last divisor
}
// Renvoie le PGCD de a et b, et chaque étape de division.
function euclidSteps(a, b) {
let x = Math.abs(a);
let y = Math.abs(b);
if (x < y) [x, y] = [y, x]; // x est le plus grand
const steps = [];
while (y > 0) {
const q = Math.floor(x / y); // combien de fois y entre dans x
const r = x % y; // le reste
steps.push({ x, y, q, r });
x = y; // le diviseur passe dans x
y = r; // le reste passe dans y
}
return { gcd: x, steps }; // x est le dernier diviseur
}
Would you like to learn to write this code yourself? Programming for Kids gets you there in seven lessons.
Tu veux apprendre à écrire ce code toi-même? La programmation pour les jeunes t’y mène en sept leçons.
Try it yourself
Essaie toi-même
Type two whole numbers from 1 to 9,999 and watch the algorithm run.
Écris deux nombres entiers de 1 à 9 999 et regarde l’algorithme travailler.
Exercises
Exercices
Work out the GCD with Euclid’s algorithm, then type only the answer.