ProgrammingHardGoldman Sachs OA · TCS Digital
What will be the output of the following pseudocode? (All variables are integers, so / is integer division)
What will be the output of the following pseudocode? (All variables are integers, so / is integer division)
Function phi(Integer n)
Integer r = n, p = 2
while (p * p <= n)
if (n mod p == 0)
while (n mod p == 0)
n = n / p
end while
r = r - r / p
end if
p = p + 1
end while
if (n > 1)
r = r - r / n
end if
return r
End Function
Print phi(36), phi(97)- A. 12 96
- B. 24 96
- C. 12 97
- D. 18 96
Correct answer: A. 12 96
This is Euler's totient: 36 = 2² × 3², so φ(36) = 36 × (1/2) × (2/3) = 12; 97 is prime, so φ(97) = 96. Output 12 96.
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? Integer s = 0 for (each i from 1 to 99) if (i mod 2 == 1) s = s + i else s = s - i…
- What will be the output of the following pseudocode? (All variables are integers, so / is integer division) Integer n = 100, steps = 0…
- What will be the output of the following pseudocode? (All variables are integers, so / is integer division) Integer dp[0..10] dp[1] = 0…
- What will be the output of the following pseudocode? (& is bitwise AND on two's-complement integers) Integer c = 0 for (each i from 0 to…
- What will be the output of the following pseudocode? (^ is XOR, & is AND; x & (−x) keeps the lowest 1-bit) Integer a[6] = {2, 4, 7, 9, 2…
- What will be the output of the following pseudocode? String w[3] = {"flower", "flow", "flight"} String p = w[0] for (each i from 1 to 2)…