Time Complexity of the Given C++ gcd Function
The provided C++ function is an iterative form of the Euclidean algorithm that repeatedly replaces with until , returning the final .
Algorithmically, this is the same core recurrence as the Euclidean algorithm:
and the loop continues while . In the worst case (e.g., when inputs are consecutive Fibonacci numbers), the number of iterations grows on the order of (more precisely ), so for it is .
Therefore, the correct option is (i) .
Key learning terms:
- Euclidean algorithm
- Modulus operation
- Loop iteration
- Worst-case complexity
Note: The complexity discussed below counts loop iterations; each iteration performs a constant amount of arithmetic and swaps.
Euclidean GCD Algorithm - Easy Explanation and Implementation
Why the loop runs times (for )
Let . Each iteration replaces with , where
0 \le n \bmod m < m. $$] So the second value strictly decreases, and the algorithm terminates when $m$ becomes $0$. A key worst-case insight is that the Euclidean algorithm decreases the numbers “slowly” when quotients of the division are as small as possible, which happens for inputs that follow a Fibonacci-like structure. In that worst case, the number of remainder steps grows proportionally to the index of the Fibonacci number, which is logarithmic in the magnitude of the input. Thus for $n>m$, the worst-case number of iterations is\Theta(\log n),
and the total runtime is also $\Theta(\log n)$ because each iteration costs $O(1)$.Time-complexity reasoning for the Euclidean-style gcd loop
- 1Step 1
Inside the loop, the code does and then swaps, implementing the transformation .
- 2Step 2
After computing , the new remainder satisfies , so the next loop's is smaller than the previous .
- 3Step 3
The largest number of iterations occurs when the remainder decreases as slowly as possible, which is known to correspond to a Fibonacci/worst-case Euclid pattern.
- 4Step 4
Because each loop iteration does constant work (one modulus, one swap, comparisons), runtime is proportional to the number of iterations.
- 5Step 5
For , the iteration count is , hence total time complexity is .
Pro Tip
When analyzing Euclidean-style code, focus on how the pair transforms: if the second argument decreases in a pattern tied to Fibonacci numbers, the runtime becomes logarithmic.
Common pitfall
Don’t treat as “reducing by a constant fraction every time.” Sometimes it reduces slowly; worst-case Euclid is still only logarithmic, but the proof requires the Euclid/Fibonacci argument.
Matching options to expected gcd runtime
Euclidean algorithm worst-case iteration count is logarithmic in input magnitude.
Exam-style clarification
Knowledge Check
For the given gcd implementation (iterative Euclidean algorithm), assuming , what is the worst-case time complexity?
Explore Related Topics
Counting Relations on a Set with $n$ Elements
Matrix Chain Multiplication with Dynamic Programming: Optimal Parenthesization for $\{4,10,3,12,20,7\}$
The lesson shows how dynamic programming determines the cheapest way to multiply the matrix chain with dimensions .
- The recurrence with computes optimal sub‑costs.
- Filling the cost table yields , and the split table gives the top‑level split .
- The optimal parenthesization is , requiring scalar multiplications.
- The bottom‑up algorithm runs in time and uses space.
Bipartite Testing When No Odd-Length Cycles Exist: Best Known Time Complexity