ProgrammingVery hardCAT · MBA placement
A pair (a, b) with 1 <= a < b <= 20 is called coprime if gcd(a, b) = 1. The procedure below counts coprime pairs. What is printed?
A pair (a, b) with 1 <= a < b <= 20 is called coprime if gcd(a, b) = 1. The procedure below counts coprime pairs. What is printed?
count = 0
for a = 1 to 20
for b = a + 1 to 20
if gcd(a, b) == 1 then count = count + 1
end for
end for
Print count- A. 128
- B. 63
- C. 190
- D. 127
Correct answer: D. 127
Counting every pair with a < b and gcd 1 gives 127. Including a = b adds the diagonal and gives 128, counting the non-coprime pairs gives 63, and counting all pairs ignores the test and gives 190.
Practise more Pseudocode and Code Tracing questionsPseudocode and Code Tracing: notes and key points
More Pseudocode and Code Tracing questions
- The sum of the divisors of N = 28 is computed by the procedure below. A number is perfect when its proper divisors (excluding N) add up to…
- The procedure below counts the digits of 2^10 by repeated division by 10. What is printed? N = 2^10; d = 0 while N > 0 N = N div 10; d = d…
- 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…
- 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…