GCD and LCM Euclidean Algorithm in DSA C# - Time & Space Complexity
We want to understand how fast the Euclidean algorithm finds the greatest common divisor (GCD) of two numbers.
How does the number of steps grow as the input numbers get bigger?
Analyze the time complexity of the following code snippet.
int GCD(int a, int b) {
while (b != 0) {
int temp = b;
b = a % b;
a = temp;
}
return a;
}
int LCM(int a, int b) {
return (a / GCD(a, b)) * b;
}
This code finds the GCD using the Euclidean algorithm and then calculates the LCM using the GCD.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: The while loop repeatedly calculates the remainder (a % b).
- How many times: The loop runs until b becomes zero, which depends on how quickly the numbers reduce.
The number of steps depends on how many times we can replace the pair (a, b) with smaller numbers using the remainder.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | Up to 5 steps |
| 100 | Up to 10 steps |
| 1000 | Up to 15 steps |
Pattern observation: The steps grow slowly, roughly proportional to the number of digits, not the value itself.
Time Complexity: O(log min(a, b))
This means the time to find the GCD grows slowly, roughly with the number of digits of the smaller input.
[X] Wrong: "The Euclidean algorithm takes time proportional to the input values themselves (like O(n))."
[OK] Correct: The algorithm reduces the numbers quickly by using remainders, so the steps grow with the number of digits (logarithmically), not the full value.
Understanding this algorithm's time complexity shows you can analyze efficient number-based algorithms, a useful skill for many coding problems.
"What if we used subtraction instead of remainder in the GCD calculation? How would the time complexity change?"
Practice
a and b?Solution
Step 1: Understand the Euclidean algorithm purpose
The Euclidean algorithm is designed to find the greatest common divisor (GCD) of two numbers by repeatedly applying the remainder operation.Step 2: Differentiate GCD from LCM
While GCD finds the largest number dividing both, LCM finds the smallest number divisible by both. The Euclidean algorithm directly calculates GCD.Final Answer:
The Greatest Common Divisor (GCD) -> Option BQuick Check:
Euclidean algorithm = GCD [OK]
- Confusing GCD with LCM
- Thinking it calculates sum or difference
- Assuming it finds prime factors directly
a and b using the built-in method?Solution
Step 1: Identify the correct method name in C#
The .NET 6+ Math class provides the methodMath.Gcd(int, int)to calculate the greatest common divisor.Step 2: Check method name casing and spelling
Method names in C# are PascalCase, soMath.Gcdis correct, while others are incorrect or do not exist.Final Answer:
int gcd = Math.Gcd(a, b); -> Option DQuick Check:
Correct method is Math.Gcd [OK]
- Using incorrect method names like Math.gcd or Math.GCD
- Assuming method is GreatestCommonDivisor
- Wrong casing causing compile errors
int a = 48;
int b = 18;
int gcd = Math.Gcd(a, b);
int lcm = (a * b) / gcd;
Console.WriteLine($"GCD: {gcd}, LCM: {lcm}");
Solution
Step 1: Calculate GCD of 48 and 18
Using Euclidean algorithm: 48 % 18 = 12, 18 % 12 = 6, 12 % 6 = 0, so GCD = 6.Step 2: Calculate LCM using formula
LCM = (48 * 18) / 6 = 864 / 6 = 144.Final Answer:
GCD: 6, LCM: 144 -> Option CQuick Check:
GCD=6, LCM=144 [OK]
- Calculating GCD incorrectly
- Swapping GCD and LCM values
- Forgetting to divide product by GCD for LCM
int Gcd(int a, int b) {
while (b != 0) {
int temp = a % b;
a = b;
b = temp;
}
return b;
}Solution
Step 1: Trace the Euclidean algorithm loop
The loop updatesaandbuntilbbecomes zero. At loop end,aholds the GCD.Step 2: Check return statement correctness
The code returnsb, which is zero at loop end. It should returnawhich holds the GCD.Final Answer:
Returnsbinstead ofaafter loop ends -> Option AQuick Check:
Returnaafter loop [OK]
- Returning the wrong variable after loop
- Confusing remainder operator with division
- Not updating variables inside loop
A = [12, 15, 21] and B = [18, 35, 14], which C# code snippet correctly computes an array of LCMs for each pair (A[i], B[i]) using the Euclidean algorithm?Solution
Step 1: Calculate GCD for each pair
UseMath.Gcd(A[i], B[i])to find the greatest common divisor for each pair.Step 2: Compute LCM using formula
LCM = (A[i] * B[i]) / GCD. Store this in thelcmsarray at indexi.Final Answer:
Option A code snippet correctly computes LCM array -> Option AQuick Check:
LCM = (a*b)/GCD for each pair [OK]
- Multiplying GCD with product instead of dividing
- Assigning GCD instead of LCM to result array
- Using sum instead of product in LCM formula
Ready to prove it?
Pass the test and this pattern moves unknown
