ProgrammingVery hardTCS NQT · Accenture

The procedure below finds how many integers x with 1 <= x <= 200 satisfy x^2 mod 13 == 1. What is printed?

The procedure below finds how many integers x with 1 <= x <= 200 satisfy x^2 mod 13 == 1. What is printed?
count = 0
for x = 1 to 200
    if (x * x) mod 13 == 1 then count = count + 1
end for
Print count
  1. A. 16
  2. B. 15
  3. C. 31
  4. D. 30

Correct answer: C. 31

The residues with x^2 = 1 mod 13 are x = 1 and x = 12 (mod 13). Up to 200 there are 15 full cycles (30 values) plus x = 196, giving 31. Testing only x = 1 mod 13 gives 16, restricting to odd x gives 15, and stopping at 195 gives 30.

More Pseudocode and Code Tracing questions

Exams that ask this topic