Data Structures & AlgorithmsVery hardICPC regional · modular arithmetic

Problem: How many integers x with 1 <= x <= 100 satisfy x^3 ≡ x (mod 7)?

Problem: How many integers x with 1 <= x <= 100 satisfy x^3 ≡ x (mod 7)?
print(sum(1 for x in range(1, 101) if pow(x, 3, 7) == x % 7))
What is printed?
  1. A. 44
  2. B. 43
  3. C. 29
  4. D. 100

Correct answer: B. 43

x³ ≡ x (mod 7) only for residues 0, 1 and 6 (cubes mod 7 are 0 or ±1), and [1, 100] holds 14 + 15 + 14 = 43 such numbers. Starting the range at 0 gives 44, forgetting residue 0 (x³ − x = x(x − 1)(x + 1)) gives 29, and confusing this with Fermat's x⁷ ≡ x gives 100.

More Coding Interview Problems questions

Exams that ask this topic