ProgrammingVery hardCAT · MBA placement

Let phi(n) be the number of integers i with 1 <= i <= n that share no common factor with n except 1. The procedure below computes phi(36) by testing every i with gcd(i, n) = 1. What is printed?

Let phi(n) be the number of integers i with 1 <= i <= n that share no common factor with n except 1. The procedure below computes phi(36) by testing every i with gcd(i, n) = 1. What is printed?
count = 0
for i = 1 to n
    if gcd(i, n) == 1 then count = count + 1
end for
Print count
  1. A. 9
  2. B. 11
  3. C. 12
  4. D. 24

Correct answer: C. 12

The integers below 36 coprime to it are those not divisible by 2 or 3, giving 12. Counting divisors of 36 gives 9, starting the range at 2 misses the value 1 and gives 11, and counting the non-coprime integers gives 24.

More Pseudocode and Code Tracing questions

Exams that ask this topic