Data Structures & AlgorithmsModerateCodeforces Div2 A · summation

Problem: What is the sum of i (5 - i) for i from 1 to 4? Run the code.

Problem: What is the sum of i * (5 - i) for i from 1 to 4? Run the code.
print(sum(i * (5 - i) for i in range(1, 5)))
What is printed?
  1. A. 30
  2. B. 15
  3. C. 24
  4. D. 20

Correct answer: D. 20

The terms are 4, 6, 6, 4, which sum to 20. Miscounting one term (for example using 6 for i = 1) gives a wrong total.

More Coding Interview Problems questions

Exams that ask this topic