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?
- A. 13
- B. 14
- C. 18
- 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
- Problem: In how many ordered ways can 10 be written as a sum of parts from {1, 2, 3}, where the order of the parts matters? Run the code…
- Problem: How many subsets (including the empty subset) of [1, 2, 3] have XOR equal to 0? Run the code. python a = [1, 2, 3] print(sum(1…
- Problem: What does the following print? python print(bin(10).count('1'), (10).bitlength())
- Problem: How many pairs (i < j) of [2, 3, 4, 5, 6] have gcd(a[i], a[j]) = 1? Run the code. python from math import gcd a = [2, 3, 4, 5, 6]…
- What does this Python program print? python a = [2, -2, 3, -3, 1, 1] p = {0: 1}; s = 0; ans = 0 for x in a: s += x ans += p.get(s, 0) p[s]…
- What does this program print? python from collections import Counter a = [3, 1, 2, 3, 1, 2, 3] c = Counter(a) pairs = sum(v (v - 1) // 2…