ProgrammingHardTCS NQT Advanced · Goldman Sachs OA
What will be the output of the following pseudocode?
What will be the output of the following pseudocode?
Function gcd(Integer a, Integer b)
if (b == 0) return a
return gcd(b, a mod b)
End Function
Integer c = 0
for (each i from 1 to 10)
for (each j from i + 1 to 10)
if (gcd(i, j) == 1) c = c + 1
end for
end for
Print c- A. 45
- B. 32
- C. 27
- D. 31
Correct answer: D. 31
c counts pairs i < j up to 10 with no common factor. For each j, the number of smaller coprime i is φ(j): 1 + 2 + 2 + 4 + 2 + 6 + 4 + 6 + 4 = 31 (j = 2 to 10).
Practise more Pseudocode and Code Tracing questionsPseudocode and Code Tracing: notes and key points
More Pseudocode and Code Tracing questions
- What will be the output of the following pseudocode? String s = "hello world of code" Integer changes = 0 for (each i from 0 to length(s)…
- What will be the output of the following pseudocode? (strings are indexed from 0) String s = "ABCABCAB" Integer n = length(s), p = n for…
- What will be the output of the following pseudocode? (strings are indexed from 0; + joins strings) String s = "ABCDEF", r = "" Integer k =…
- What will be the output of the following pseudocode? Integer a[7] = {1, 3, 2, 4, 1, 5, 2}, c = 0 for (each i from 1 to 5) if (a[i] > a[i…
- What will be the output of the following pseudocode? Integer a[5] = {1, 2, 3, 4, 5} Integer odd = 0, even = 1, s = 0, ans = 0 // counts of…
- What will be the output of the following pseudocode? (& is bitwise AND on 32-bit integers) Function pow4(Integer n) return n > 0 AND (n &…