Lesson goal: Sum of consecutive integer sets and triangular numbers

Previous: Linear Diophantine equations | Home | Next: Sum of all digits from 1 to 10,000

In the 1967 MAA High School Mathematics Contest (Problem #39), contestants encountered an intriguing sequence of number sets:

"Given the sets of consecutive integers $\{1\}$, $\{2, 3\}$, $\{4, 5, 6\}$, $\{7, 8, 9, 10\}, \dots$, where each set contains one more element than the preceding one. What is the sum of the elements in the 20th set?"

Let's analyze the pattern:
  • Set 1: $\{1\}$, length = 1, sum = 1
  • Set 2: $\{2, 3\}$, length = 2, sum = 5
  • Set 3: $\{4, 5, 6\}$, length = 3, sum = 15
  • Set 4: $\{7, 8, 9, 10\}$, length = 4, sum = 34
Notice where each set begins! Before Set $n$, all previous sets have used up a number of integers equal to the $(n-1)$-th triangular number: $$T_{n-1} = 1 + 2 + 3 + \dots + (n - 1) = \frac{(n-1)n}{2} = \frac{n^2 - n}{2}$$ Therefore, the first number in Set $n$ is: $$\text{first}(n) = T_{n-1} + 1 = \frac{n^2 - n + 2}{2}$$ Since Set $n$ contains $n$ consecutive numbers, its last number is $\text{first} + (n - 1) = \frac{n^2 + n}{2}$.

Using Gauss's formula for the sum of an arithmetic progression ($\text{sum} = n \times \text{average}$): $$S(n) = n \cdot \left(\frac{\text{first} + \text{last}}{2}\right) = n \cdot \left(\frac{n^2 + 1}{2}\right) = \frac{n(n^2 + 1)}{2}$$ For the 20th set ($n = 20$): $$S(20) = \frac{20(20^2 + 1)}{2} = 10 \cdot 401 = 4010$$ In this lesson, we write nested loops to generate these sets, sum them up, and confirm our algebraic formula!
curr = 1
for s = 1, n do

  sum = 0

  for i = 1, s do

    sum = sum + curr

    curr = curr + 1
  end

end
Move the mouse over a dotted box for more information.

  • Triangular Numbers: The numbers $1, 3, 6, 10, 15, 21, \dots$ are called triangular numbers because that many dots can be arranged into an equilateral triangle. They appear constantly in combinatorics and number patterns.
  • Nicomachus's Theorem: If you partition consecutive odd integers instead: $\{1\}, \{3, 5\}, \{7, 9, 11\}, \dots$, the sum of the $n$-th set is $n^3$! (e.g. $1 = 1^3$, $3+5 = 8 = 2^3$, $7+9+11 = 27 = 3^3$).

Now you try. Run the code to see the sums for the first 5 sets and the 20th set. Then try printing the sum of the 100th set!

Type your code here:


See your results here: