Data Structures & AlgorithmsHardProduct Company SDE · CP practice

Problem: How many integers x in [1, 2000] satisfy x^2 ≡ x (mod 10)? Run the code.

Problem: How many integers x in [1, 2000] satisfy x^2 ≡ x (mod 10)? Run the code.
print(sum(1 for x in range(1, 2001) if (x * x - x) % 10 == 0))
What is printed?
  1. A. 400
  2. B. 600
  3. C. 800
  4. D. 80

Correct answer: C. 800

x(x − 1) is always even, so it needs 5 | x(x − 1), i.e. x mod 10 ∈ {0, 1, 5, 6}: 4/10 of 2000 = 800. Only 0 and 1 gives 400, missing one of 5 or 6 gives 600, and requiring x² ≡ x (mod 100) gives 80.

More Coding Interview Problems questions

Exams that ask this topic