0% found this document useful (0 votes)
2 views5 pages

Home

The document consists of a series of questions related to Python implementations of various algorithms for calculating the greatest common divisor (GCD). It covers different methods such as the Euclidean algorithm, Stein's algorithm, and the divisors method, along with their expected outputs and behaviors in specific scenarios. Each question tests understanding of the algorithms' mechanics and their edge cases.

Uploaded by

bilalmhasan855
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views5 pages

Home

The document consists of a series of questions related to Python implementations of various algorithms for calculating the greatest common divisor (GCD). It covers different methods such as the Euclidean algorithm, Stein's algorithm, and the divisors method, along with their expected outputs and behaviors in specific scenarios. Each question tests understanding of the algorithms' mechanics and their edge cases.

Uploaded by

bilalmhasan855
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Q1: What will be the output of the following Python code snippet?

def gcd_mystery(A, B): A) 12


if A % B == 0: B) Error (infinite recursion)
return B C) 36
return gcd_mystery(A - B, B) D) None of the above
print(gcd_mystery(36, 48))

Q2: How many iterations does gcd_euclidean(100, 7) take?A) 1 B) 2 C) 3 D) 4

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?

def gcd_stein(A, B):


if A == B or B == 0:
return A
if A == 0:
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))
A) A=0,B=0 B) A=10,B=5 C) A=48,B=36 D) A=1,B=1

Q6: What will be the output of the 3rd iteration of the while loop in the following
Euclidean algorithm?

def gcd_euclidean(A, B):


while B:
A, B = B, A % B
print(A, B)
gcd_euclidean(100, 7)
A) (2, 1) B) (1, 0) C) (7, 2) D) (2, 7)

Q7: Examine the Euclidean Algorithm implemented in Python:

def gcd_euclidean(A, B):


while B:
A, B = B, A % B
return A
What is the role of the line A, B = B, A % B in this function?
A) It swaps A and B without using a temporary variable.
B) It updates A to be the remainder of B divided by A.
C) It reduces the problem by setting A to B and B to the remainder of A divided by B.
D) It computes the GCD directly by taking the modulus of A and B.

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)

Q11: Analyze the implementation of Stein’s Algorithm (Binary GCD):

def gcd_stein(A, B):


if A == B or B == 0:
return A
if A == 0:
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))
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.

Q12: What will be the output of the following code?


from math import gcd A) 7, 7, 0
B) 7, 7, Error
print(gcd(0, 7)) C) Error, 7, 0
print(gcd(7, 0)) D) 7, Error, Error
print(gcd(0, 0))

Q13: Consider the Prime Factorization Method implementation:


from sympy import factorint

def gcd_prime_factorization(A, B):


factors_A = factorint(A)
factors_B = factorint(B)

common_factors = set(factors_A.keys()) & set(factors_B.keys())

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))

You might also like