ProgrammingHardTCS CodeVita · Infosys HackWithInfy

What is printed?

What is printed?
print(sum(range(1, 10 ** 5 + 1, 7)))
  1. A. 714278571
  2. B. 714300000
  3. C. 714278570
  4. D. 714278572

Correct answer: A. 714278571

range(1, 100001, 7) has 14286 terms, from 1 to 99996. Their sum is 14286 * (1 + 99996) / 2 = 714278571. Summing by formula avoids looping over 10^5 values in a judge.

More Python Programming questions

Exams that ask this topic