Home
Home
Q3: What will be the output of the 3rd recursive call in the following Python function
when called with gcd_stein(48, 36)?
def gcd_stein(A, B): A) gcd_stein(24, 18)
if A == B or B == 0: B) gcd_stein(12, 9)
return A C) gcd_stein(6, 9)
if A == 0: D) gcd_stein(3, 9)
return B
if A % 2 == 0 and B % 2 == 0:
return 2 * gcd_stein(A // 2, B // 2)
elif A % 2 == 0:
return gcd_stein(A // 2, B)
elif B % 2 == 0:
return gcd_stein(A, B // 2)
else:
return gcd_stein(abs(A - B), min(A, B))
print(gcd_stein(48, 36))
Q4: Consider the following Python function for the Divisors Method:
def find_divisors(n):
divisors = set()
for i in range(1, n + 1):
if n % i == 0:
divisors.add(i)
return divisors
def gcd_via_divisors(A, B):
divisors_A = find_divisors(A)
divisors_B = find_divisors(B)
common_divisors = divisors_A.intersection(divisors_B)
return max(common_divisors) if common_divisors else 1
What is the primary reason why this implementation uses a set to store divisors instead
of a list?
A) Sets automatically sort the divisors in ascending order.
B) Sets eliminate duplicate divisors, ensuring uniqueness.
C) Sets allow for faster intersection operations.
D) Sets consume less memory than lists for large numbers.
Q5: Which of the following inputs would cause gcd_stein(A, B) to return an incorrect
result?
Q6: What will be the output of the 3rd iteration of the while loop in the following
Euclidean algorithm?
Q8: Which of the following modifications to the Euclidean algorithm will break its
correctness?
def gcd_euclidean(A, B):
while B:
A, B = A, A % B # Modified line
return A
A) The algorithm still works correctly. B) The algorithm runs indefinitely.
C) The algorithm produces incorrect results for all inputs.
D) The algorithm produces incorrect results for some inputs.
Q9: If an unknown GCD function takes 20 iterations for gcd(10^9, 1), which algorithm is
it likely using?
A) Divisors Method B) Euclidean Algorithm
C) Binary GCD Algorithm (Stein’s) D) Prime Factorization
Q10: What will be the output of the 2nd iteration of the while loop in the following
Euclidean algorithm?
def gcd_euclidean(A, B): A) (7, 2)
while B: B) (2, 1)
A, B = B, A % B C) (1, 0)
print(A, B) D) (7, 100)
gcd_euclidean(100, 7)
if A % 2 == 0 and B % 2 == 0:
return 2 * gcd_stein(A // 2, B // 2)
elif A % 2 == 0:
return gcd_stein(A // 2, B)
elif B % 2 == 0:
return gcd_stein(A, B // 2)
else:
return gcd_stein(abs(A - B), min(A, B))
What is the purpose of the condition if A % 2 == 0 and B % 2 == 0 in this algorithm?
A) It checks if both numbers are even to factor out a 2 and recurse on smaller numbers.
B) It determines if both numbers are odd to subtract the smaller from the larger.
C) It verifies if both numbers are divisible by 2 to simplify the GCD calculation.
D) It ensures that the algorithm terminates when both numbers are even.
gcd_value = 1
for factor in common_factors:
gcd_value *= factor ** min(factors_A[factor], factors_B[factor])
return gcd_value
Why does this function use set(factors_A.keys()) & set(factors_B.keys()) to find
common prime factors?
A) To ensure that only unique prime factors are considered.
B) To efficiently compute the intersection of prime factors using set operations.
C) To convert the dictionary keys into a list for easier iteration.
D) To sort the prime factors in ascending order.
Q14: What is the significance of starting the loop from min(A, B) and decrementing to 1,
in following python code?
def gcd_naive(A, B):
for i in range(min(A, B), 0, -1):
if A % i == 0 and B % i == 0:
return i
A) It ensures that the largest common divisor is found first.
B) It reduces the number of iterations needed to find the GCD.
C) It avoids checking numbers larger than both A and B.
D) It guarantees that the loop terminates when i reaches 0.
Q15: What will be the output of the 2nd recursive call in the following Python code?
def gcd_mystery(A, B): A) gcd_mystery(36, 48) → gcd_mystery(-12, 48)
if A % B == 0: (Infinite Recursion)
return B B) gcd_mystery(36, 48) → gcd_mystery(36, 12)
return gcd_mystery(A - B, C) gcd_mystery(36, 48) → gcd_mystery(24, 48)
B) D) gcd_mystery(36, 48) → gcd_mystery(36, 36)
print(gcd_mystery(36, 48))