Data Structures & AlgorithmsModerateCodeforces Div2 B · modular arithmetic

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

Problem: How many integers x in [1, 100] satisfy x^2 ≡ 1 (mod 8)? Run the code.
print(sum(1 for x in range(1, 101) if (x * x) % 8 == 1))
What is printed?
  1. A. 50
  2. B. 25
  3. C. 75
  4. D. 100

Correct answer: A. 50

x^2 ≡ 1 (mod 8) holds exactly for odd x, and there are 50 odd numbers in [1, 100].

More Coding Interview Problems questions

Exams that ask this topic