Euclidean algorithm
Appearance
The Euclidean algorithm is an algorithm. It can be used to find the biggest number that divides two other numbers (the greatest common divisor of two numbers).
What the algorithm looks like in words
Euclid solved the problem graphically. He said
- If you have two distances, AB and CD, and you always take away the smaller from the bigger, you will end up with a distance that measures both of them.
The algorithm as an enumerated list
Start out with two integers a and b. Let m have the same value as a, and n have the same value as b.
- If the value of m is less than the value of n, switch the values of m and n
- Find a number r equal to m minus n
- Let m have the same value as n, and n have the same value as r
- If r does not have the value of 0, go to step 1
- The wanted value is in m.
The algorithm in pseudocode
Given: two integers m and n
Output: the greatest common integer divisor of m and n
if m < n, swap m and n Let r = -1 while r does not equal 0 r = m mod n m = n n = r output n