Data Structures & AlgorithmsModerateTCS NQT · counting

Problem: How many integers in [1, 100] are not perfect squares? Run the code.

Problem: How many integers in [1, 100] are not perfect squares? Run the code.
print(sum(1 for x in range(1, 101) if int(round(x ** 0.5)) ** 2 != x))
What is printed?
  1. A. 89
  2. B. 91
  3. C. 90
  4. D. 10

Correct answer: C. 90

There are 10 perfect squares from 1 to 100 (1, 4, ..., 100), so 90 numbers are not squares.

More Coding Interview Problems questions

Exams that ask this topic