New: 201 pattern programs with tested code in C, C++, Java and Python Explore patterns

Increasing Difference Number Triangle (1, 2 4, 7 11 16)

Read summary with AI tools

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
  1. Increasing Difference Number Triangle output
  2. How the increasing difference number triangle works
  3. Increasing Difference Number Triangle in C
  4. Increasing Difference Number Triangle in C++
  5. Increasing Difference Number Triangle in Java
  6. Increasing Difference Number Triangle in Python
  7. Time and space complexity
  8. 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
Increasing Difference Number Triangle layout diagram, row by row (n = 5)
The numbers of the increasing difference number triangle laid out row by row (n = 5).

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.

  1. Set value = 1 and gap = 1.
  2. Run the outer loop for i from 1 to n.
  3. Repeat i times: print value four characters wide, add gap to value, then add 1 to gap.
  4. 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 is k(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.

Authored by

Ghanendra Yadav runs Pattern Programs from Bangalore, India. The site started in 2020 to help students practise loops and logic through pattern programs in C, C++, Java and Python. Every program published here is compiled and run before it goes live, and the output shown is the real output.

Latest articles

Leave a comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.