Which approach to summing 1 to n is most efficient?

  1. The formula n(n + 1)/2
  2. Recursion adding one at a time
  3. Two nested loops
  4. A loop adding each number
Answer

A) The formula n(n + 1)/2

The formula needs a single calculation whatever the size of n. SLO CS-11-B-01

Computer Science MCQ · More Unit 2: Computational Thinking and Algorithms MCQs · Practise