While creating an algorithm to compute the greatest common divisor of two numbers I saw that — on various websites/books — when you have "gcd(a,b)" a is superior to b ; is it an obligation or am I right if I write gcd(a,b) where a < b ?
2026-03-28 01:14:59.1774660499
Is it correct to write gcd(a,b) if a<b?
100 Views Asked by Bumbble Comm https://math.techqa.club/user/bumbble-comm/detail At
4
Yes $gcd(a, b) = gcd(b, a)$. But if you are writing a function that calculates gcd, ensure that the divider is smaller one, else you will waste more cycles. See the algorithm below.
From Donald Knuth (A popular Computer Scientist and the inventor of Tex):
Wasted Cycle example (without E0 step):
Let $a = 3$ and $b = 10$.
$$r \leftarrow a \bmod b$$ Thus, $r = a$
$$a \leftarrow b, b \leftarrow r$$
Now that $a>b$ the algorithm can compute the "real answer", but with the price of wasting cycles.