Python 3.11+
The code uses standard Python syntax and containers supported by the browser Judge. The Euclidean GCD proof does not depend on a minor Python release.
Verify in Python docs(opens in a new tab)Repeatedly replace a pair by divisor and remainder to compute the greatest common divisor. Learn the algorithm's preconditions, state transition, correctness argument, Python cost model, and transfer from one focused drill to a full Interview Problem.
Recognize it when
Consider Euclidean GCD when the prompt's constraints and required operations match this shape: Repeatedly replace a pair by divisor and remainder to compute the greatest common divisor.

Sign in to save your code and progress across devices. The lesson and problem statement remain public.
The common divisors of a and b are exactly the common divisors of b and a modulo b. This identity lets Euclid replace the pair with a strictly smaller second value.
Repeat a, b = b, a mod b until b is zero. The last nonzero a is the greatest common divisor, and the remainder decrease proves logarithmic termination.
def gcd_trace(a,b):
a,b=abs(a),abs(b);trace=[]
while b:trace.append([a,b,a%b]);a,b=b,a%b
return traceImplement gcd_trace(a,b). Normalize signs and return [a,b,a%b] until b becomes zero.
Loading interactive editor.If it does not appear, the reference and starter code remain readable.Reload editor
Return a nonnegative gcd by taking absolute values. gcd(a, 0) is abs(a), and gcd(0, 0) is conventionally zero in programming libraries.
def gcd_many(values):
result=0
for value in values:
a,b=result,abs(value)
while b:a,b=b,a%b
result=a
return resultImplement gcd_many(values). Return the nonnegative greatest common divisor, with 0 for empty input.
Loading interactive editor.If it does not appear, the reference and starter code remain readable.Reload editor
Use lcm(a,b) = abs(a // gcd(a,b) * b), guarding the all-zero case and dividing before multiplication. GCD also reduces fractions and normalizes ratios.
Say: “Replacing (a,b) with (b,a mod b) preserves every common divisor while shrinking the second argument.” Trace one remainder sequence, normalize signs, and state O(log min(a,b)) time.
Python 3.11+
The code uses standard Python syntax and containers supported by the browser Judge. The Euclidean GCD proof does not depend on a minor Python release.
Verify in Python docs(opens in a new tab)Interview bounds depend on the stated representation, input model, and real Python operations.
| Operation | Average | Worst | Interview note |
|---|---|---|---|
| Euclidean GCD complete workflow | O(log min(a,b)) | O(log min(a,b)) | Normalize signs and repeatedly replace (a,b) by (b,a mod b). |
O(1) for the focused Compute GCD with Euclid implementation.
These are the mistakes most likely to survive a happy-path example and fail a boundary case.
Verify algorithm preconditions such as sorted input, nonnegative weights, acyclicity, or admissible heuristics before applying it.
Prevent it: State and verify this precondition before coding: The integer domain and divisibility, primality, or congruence contract are explicit.
Avoid
def greatest_common_divisor(left, right):
passUse instead
def greatest_common_divisor(left, right):
left = abs(left)
right = abs(right)
while right:
left, right = right, left % right
return leftWhere you will hit this: Compute GCD with Euclid(opens in a new tab)
Returns the smaller input instead of reducing by remainders.
Prevent it: Preserve this proof obligation: Each transformation preserves the required number-theoretic relation while moving toward a terminal bound.
Avoid
def greatest_common_divisor(a,b):
return min(abs(a),abs(b))Use instead
def greatest_common_divisor(left, right):
left = abs(left)
right = abs(right)
while right:
left, right = right, left % right
return leftWhere you will hit this: Compute GCD with Euclid(opens in a new tab)
Include visited state, boundary cases, recursion depth, and hidden copying costs in correctness and complexity analysis.
Prevent it: Count every sort, slice, copy, membership check, heap update, and recursive frame before claiming O(log min(a,b)).
Avoid
def greatest_common_divisor(a,b):
return min(abs(a),abs(b))Use instead
def greatest_common_divisor(left, right):
left = abs(left)
right = abs(right)
while right:
left, right = right, left % right
return leftWhere you will hit this: Insert GCD Nodes in a Linked List(opens in a new tab)
Official Python documentation supports language behavior. Canonical problem pages provide additional practice context.
python-docs · checked 2026-07-27