AlgoViz

Pattern 13

Easy

Continuously increasing numbers

Problem

Print a triangle of continuously increasing numbers for a given size n.

In simple words

Use one loop for the rows and another for what goes in each row to draw the shape.

The idea

Unlike the earlier triangles, the number here does not reset each row — it keeps counting across the whole shape. That means the counter lives outside both loops.

The trick

  • Declare the counter before the outer loop, never inside it.
rows5

Step 1 of 7. Numbers keep counting up across the whole triangle. rows 5.

1/7
Optimal
timeO(n^2)spaceO(1)
1// n = 5 produces:2// 13// 2 34// 4 5 65// 7 8 9 106// 11 12 13 14 157 8c = 19for i in 1..n:10  for j in 1..i: print(c); c++11  newline

Input

grid
5 × 5

Memory

rows
5

Output

printed rows
rows
5

Check yourself

3 quick questions about this walkthrough. A wrong answer costs nothing.

Example

Input:
n = 5
Output:
1 / 2 3 / 4 5 6 / 7 8 9 10 / 11 12 13 14 15

Finished the walkthrough? Add it to your streak.