ProgrammingHardTCS NQT · Accenture
Euclid's algorithm finds gcd(48, 18) by replacing the pair (a, b) with (b, a mod b) until b is 0. The loop below counts how many times the replacement happens. What is printed?
Euclid's algorithm finds gcd(48, 18) by replacing the pair (a, b) with (b, a mod b) until b is 0. The loop below counts how many times the replacement happens. What is printed?
count = 0
while b != 0
(a, b) = (b, a mod b); count = count + 1
end while
Print count- A. 6
- B. 5
- C. 3
- D. 0
Correct answer: C. 3
The pairs go (48,18), (18,12), (12,6), (6,0), so the loop body runs 3 times. Printing the final a gives the gcd 6 rather than the count, summing the quotients gives 5, and printing the final b gives 0.
Practise more Pseudocode and Code Tracing questionsPseudocode and Code Tracing: notes and key points
More Pseudocode and Code Tracing questions
- The number of ordered pairs (x, y) of positive integers with x y = 36 is counted by the procedure below. What is printed? count = 0 for x…
- The Josephus game: n = 7 people stand in a circle numbered 1 to 7, and every third person is removed (counting continues around the…
- The procedure below sums the prime factors of N = 360 counted with multiplicity (2 counted three times, and so on) by trial division. What…
- A product of ten consecutive numbers 1 x 2 x ... x 10 is computed. The procedure below counts how many of the numbers 1 to 10 are…
- Smallest n such that n! has at least 5 trailing zeros is searched for by the procedure below, where z(n) is the number of factors 5 in n!…
- A number N = 1296 is reduced by the procedure below, which divides by the smallest prime factor each time it can and keeps a count of the…