0
0
DSA Pythonprogramming~10 mins

GCD and LCM Euclidean Algorithm in DSA Python - Interactive Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to return the greatest common divisor (GCD) of two numbers using the Euclidean algorithm.

DSA Python
def gcd(a, b):
    while b != 0:
        a, b = b, a [1] b
    return a
Drag options to blanks, or click blank then click option'
A%
B+
C-
D*
Attempts:
3 left
💡 Hint
Common Mistakes
Using addition or subtraction instead of the remainder operator.
Using multiplication which does not help find the GCD.
2fill in blank
medium

Complete the code to calculate the least common multiple (LCM) of two numbers using their GCD.

DSA Python
def lcm(a, b):
    return (a * b) // [1](a, b)
Drag options to blanks, or click blank then click option'
Amax
Bmin
Cabs
Dgcd
Attempts:
3 left
💡 Hint
Common Mistakes
Using max or min instead of gcd.
Forgetting to use integer division //.
3fill in blank
hard

Fix the error in the recursive GCD function by completing the return statement.

DSA Python
def gcd_recursive(a, b):
    if b == 0:
        return a
    else:
        return gcd_recursive(b, a [1] b)
Drag options to blanks, or click blank then click option'
A+
B%
C-
D*
Attempts:
3 left
💡 Hint
Common Mistakes
Using addition or subtraction instead of the remainder operator.
Using multiplication which is incorrect here.
4fill in blank
hard

Fill both blanks to create a function that returns a dictionary with GCD and LCM of two numbers.

DSA Python
def gcd_lcm(a, b):
    gcd_value = gcd(a, b)
    lcm_value = (a * b) [1] gcd_value
    return {'gcd': gcd_value, 'lcm': lcm_value}
Drag options to blanks, or click blank then click option'
A//
B%
C+
D*
Attempts:
3 left
💡 Hint
Common Mistakes
Using multiplication instead of division for LCM.
Forgetting to close the dictionary properly.
5fill in blank
hard

Fill all three blanks to create a dictionary comprehension that maps numbers to their GCD with a fixed number.

DSA Python
fixed_num = 12
result = [1]: gcd([2], fixed_num) for [3] in range(1, 6)}
Drag options to blanks, or click blank then click option'
Anum
Bfixed_num
Ci
Dx
Attempts:
3 left
💡 Hint
Common Mistakes
Using different variable names for key and loop variable.
Using fixed_num as the loop variable.