In the increasing difference number triangle the gap between neighbouring numbers grows by one every step: 1 to 2 is +1, 2 to 4 is +2, 4 to 7 is +3. The numbers run on from row to row, so five rows end at 106.
Quick answer: Keep two counters outside the loops: value = 1 and gap = 1. Row i prints i numbers. After each print, add gap to value and then increase gap by one. That gives 1, 2, 4, 7, 11, 16 and so on.
This program is part of the Series Patterns collection.
Table of contents8 sections
- Increasing Difference Number Triangle output
- How the increasing difference number triangle works
- Increasing Difference Number Triangle in C
- Increasing Difference Number Triangle in C++
- Increasing Difference Number Triangle in Java
- Increasing Difference Number Triangle in Python
- Time and space complexity
- Frequently asked questions
Increasing Difference Number Triangle output
Output (n = 5):
1 2 4 7 11 16 22 29 37 46 56 67 79 92 106

How the increasing difference number triangle works
The values follow a simple rule, but one that trips people up the first time: the step itself changes. Two counters handle it. value is the number to print and gap is how far the next number is. Both live outside the loops, so the sequence keeps going across rows instead of restarting. The same numbers answer a well-known puzzle: they are the most pieces you can cut a pancake into with 0, 1, 2, 3 … straight cuts, which is why they are also called the lazy caterer’s sequence.
- Set
value = 1andgap = 1. - Run the outer loop for
ifrom 1 ton. - Repeat
itimes: printvaluefour characters wide, addgaptovalue, then add 1 togap. - Move to the next line.
Key takeaways
- The difference between neighbours goes 1, 2, 3, 4…, so the numbers grow faster and faster.
- Both counters are set once, before the loops, so the series runs on from row to row.
- The
k-th number isk(k − 1) / 2 + 1, which you can use to check any value. - The order of the two updates matters: add the gap first, then increase it.
Increasing Difference Number Triangle in C
#include <stdio.h>
int main(void) {
int n = 5;
int value = 1, gap = 1;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= i; j++) {
printf("%4d", value);
value += gap;
gap++;
}
printf("\n");
}
return 0;
}Increasing Difference Number Triangle in C++
#include <iostream>
#include <iomanip>
using namespace std;
int main() {
int n = 5;
int value = 1, gap = 1;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= i; j++) {
cout << setw(4) << value;
value += gap;
gap++;
}
cout << endl;
}
return 0;
}Increasing Difference Number Triangle in Java
public class IncreasingDifferenceTriangle {
public static void main(String[] args) {
int n = 5;
int value = 1, gap = 1;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= i; j++) {
System.out.printf("%4d", value);
value += gap;
gap++;
}
System.out.println();
}
}
}Increasing Difference Number Triangle in Python
n = 5
value, gap = 1, 1
for i in range(1, n + 1):
for j in range(i):
print(f"{value:4d}", end="")
value += gap
gap += 1
print()Time and space complexity
Each number takes two additions, so the program runs in O(n²) time for n rows with O(1) extra memory.
Frequently asked questions
Can I compute each value without the gap counter?
Yes. Count the numbers printed so far in k (starting at 0) and print k(k + 1) / 2 + 1. The gap counter is easier to follow, the formula is easier to check.
What happens if I increase gap before adding it?
The first step becomes +2 instead of +1, so the series reads 1, 3, 6, 10, which is the triangular numbers plus nothing new. Keep the update order: add, then grow the gap.
Why is it called the lazy caterer’s sequence?
It gives the largest number of pieces a round cake or pancake can be cut into with a given number of straight cuts: 1 piece with no cuts, 2 with one, 4 with two, 7 with three. The name pokes fun at a caterer who cuts as few times as possible.






