Math Fundamentals18 sections · 814 units
Open in Course

Sum of First n Integers

The famous formula

The sum 1+2+3+...+n1 + 2 + 3 + ... + n equals n(n+1)2\frac{n(n+1)}{2}. This is one of the most useful formulas in competitive programming.

For example, 1+2+...+100=100×1012=50501 + 2 + ... + 100 = \frac{100 \times 101}{2} = 5050. You get the answer without a loop.

Why does this work? Pair the first and last terms: 1+100=1011 + 100 = 101, 2+99=1012 + 99 = 101, and so on. You have 5050 pairs, each summing to 101101, giving 50×101=505050 \times 101 = 5050. The formula generalizes this pairing trick.