Data Structures & AlgorithmsModerateCodeforces Div2 A · number theory
Problem: How many iterations does Euclid's algorithm take for gcd(1071, 462)? Count the loop passes of the code.
Problem: How many iterations does Euclid's algorithm take for gcd(1071, 462)? Count the loop passes of the code.
a, b = 1071, 462
steps = 0
while b:
a, b = b, a % b
steps += 1
print(steps)What is printed?- A. 4
- B. 2
- C. 5
- D. 3
Correct answer: D. 3
The remainders are 147, 21 and 0, so the loop runs three times. The gcd is 21.
More Coding Interview Problems questions
- Problem: From the left, how many buildings of heights [3, 1, 4, 2, 5] are visible (a building is visible if it is strictly taller than all…
- Problem: For the sorted array [1, 2, 2, 2, 5, 7], how many values are at most 2? Run the code. python import bisect a = [1, 2, 2, 2, 5, 7]…
- Problem: How many strings of length 3 over the alphabet {a, b, c} have no two equal adjacent characters? Run the code. python from…
- Problem: How many divisors does 720 have? Run the code. python print(sum(1 for d in range(1, 721) if 720 % d == 0)) What is printed?
- Problem: How many ways are there to decode the digit string '111' (1 maps to A, ..., 26 maps to Z)? Run the DP. python s = '111' dp = [1]…
- Problem: What is the length of the longest substring without repeating characters in 'pwwkew'? Run the sliding-window code. python s =…