0
0
DSA Pythonprogramming~20 mins

GCD and LCM Euclidean Algorithm in DSA Python - Practice Problems & Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Euclidean Algorithm Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of Euclidean GCD function
What is the output of this Python code that calculates the GCD of two numbers using the Euclidean algorithm?
DSA Python
def gcd(a, b):
    while b != 0:
        a, b = b, a % b
    return a

print(gcd(48, 18))
A6
B12
C18
D3
Attempts:
2 left
💡 Hint
Remember the Euclidean algorithm repeatedly replaces the larger number by the remainder until the remainder is zero.
Predict Output
intermediate
2:00remaining
Output of LCM calculation using GCD
What is the output of this Python code that calculates the LCM of two numbers using their GCD?
DSA Python
def gcd(a, b):
    while b:
        a, b = b, a % b
    return a

def lcm(a, b):
    return a * b // gcd(a, b)

print(lcm(15, 20))
A300
B35
C60
D5
Attempts:
2 left
💡 Hint
LCM can be found by multiplying the numbers and dividing by their GCD.
🧠 Conceptual
advanced
2:00remaining
Why does the Euclidean algorithm work for GCD?
Which statement best explains why the Euclidean algorithm correctly finds the greatest common divisor (GCD) of two numbers?
ABecause the GCD of two numbers also divides their difference, so repeatedly replacing the larger number by the remainder preserves the GCD.
BBecause the algorithm adds the two numbers repeatedly until they become equal, which is the GCD.
CBecause it finds the smallest number that divides both numbers without remainder.
DBecause it multiplies the two numbers and then divides by their sum to find the GCD.
Attempts:
2 left
💡 Hint
Think about how divisors relate to remainders.
Predict Output
advanced
2:00remaining
Output of recursive GCD function with negative input
What is the output of this recursive Python function when called with gcd(-24, 18)?
DSA Python
def gcd(a, b):
    if b == 0:
        return abs(a)
    else:
        return gcd(b, a % b)

print(gcd(-24, 18))
A-6
B6
C12
D0
Attempts:
2 left
💡 Hint
The function returns the absolute value of 'a' when 'b' is zero.
🧠 Conceptual
expert
2:00remaining
Time complexity of Euclidean algorithm for GCD
What is the time complexity of the Euclidean algorithm for finding the GCD of two integers a and b (where a ≥ b > 0)?
AO(1)
BO(b)
CO(a * b)
DO(log b)
Attempts:
2 left
💡 Hint
The algorithm reduces the problem size roughly by a factor related to the Fibonacci sequence.