Data Structures & AlgorithmsModerateTCS NQT · number puzzle

Problem: How many Armstrong numbers (numbers equal to the sum of their digits each raised to the power of the digit count) lie in [1, 9999]? Run the code.

Problem: How many Armstrong numbers (numbers equal to the sum of their digits each raised to the power of the digit count) lie in [1, 9999]? Run the code.
print(sum(1 for n in range(1, 10000) if n == sum(int(d) ** len(str(n)) for d in str(n))))
What is printed?
  1. A. 13
  2. B. 14
  3. C. 18
  4. D. 16

Correct answer: D. 16

Armstrong numbers are 1 to 9 (nine of them), 153, 370, 371, 407, 1634, 8208 and 9474, for 16 in total. Forgetting that 1 to 9 are trivially included gives 15 or 7.

More Coding Interview Problems questions

Exams that ask this topic