ProgrammingHardTCS CodeVita · Infosys HackWithInfy
What is printed?
What is printed?
print(sum(range(1, 10 ** 5 + 1, 7)))
- A. 714278571
- B. 714300000
- C. 714278570
- 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
- What is printed? python print(pow(7, 222, 13), pow(7, 6, 13))
- What is printed? python print(max(map(len, ['ab', 'cde', 'f'])), sorted([[2, 1], [1, 5], [1, 2]]))
- What is printed? python print(list(enumerate('ab', start=5)))
- What is printed? python print(int(3.99999999999999999), int(-0.9))
- What is printed? python print('{0}{1}{0}'.format('a', 'b'), '{:>3}'.format(7), '%%%d' % 5)
- What is printed? python print(bin(5 << 2), 5 >> 1, -5 >> 1, -5 // 2)