Shape Rule
Jump sequence
Row 1 prints 1, row 2 prints 2 6, row 3 prints 3 7 10, and so on with shrinking jumps.

The increasing jump number triangle starts each row at i and jumps forward with a decreasing step m — a natural step after the continuous counter in Program 20. This tutorial covers the shape rule, step logic, a live preview, algorithm steps, worked PHP examples, edge cases, and complexity.
Jump sequence
Row 1 prints 1, row 2 prints 2 6, row 3 prints 3 7 10, and so on with shrinking jumps.
1..rows
for ($i = 1; $i <= $rows; $i++) makes row i print exactly i numbers.
m-- each jump
Set $m = $rows - 1 and $k = $i + $m; after each print do $m-- then $k = $k + $m.
Same line / next line
Print i first, then k values in the inner loop; end each row with echo PHP_EOL.
1–15 rows
Pick a row count and draw the jump number triangle instantly in the browser.
Complexity
Total prints = rows(rows+1)/2; extra memory stays O(1).
An increasing jump number triangle prints each row starting at the row index, then jumps forward using a step that shrinks after every print. With rows = 5, the output is 1, 2 6, 3 7 10, 4 8 11 13, 5 9 12 14 15.
In PHP you print $i first, set $m = $rows - 1 and $k = $i + $m, then in the inner loop print $k, do $m--, and update $k = $k + $m before the next value.
It combines nested loops with a changing step variable — a step up from Program 20’s simple counter.
Print i before the inner loop on every row.
Start at rows - 1 and decrease after each jump.
$k = $i + $m first, then $m-- and $k = $k + $m in the loop.
Follow Program 20; continue to Program 22 (odd-length rows) next.
In short: for each row i, print i, then use a decreasing step m to compute and print the remaining i - 1 values.
Given a positive integer rows, print an increasing jump number triangle: row i starts with i, then prints i - 1 more values computed by adding a decreasing step m.
// rows = 5 (conceptual shape)
// 1
// 2 6
// 3 7 10
// 4 8 11 13
// 5 9 12 14 15 | Item | Type | Description |
|---|---|---|
rows | int | Number of triangle lines to print (typically ≥ 1). |
m | int | Step size — starts at rows - 1, decreases after each jump. |
k | int | Next value to print — set to i + m before the inner loop. |
| Printed output | text | Row i has i spaced numbers with shrinking jumps. |
for i from 1 to rows:
print i
m = rows - 1
k = i + m
for j from 1 to i - 1:
print k
m = m - 1
k = k + m
print newline | Approach | Idea | Best for |
|---|---|---|
| Decreasing step m | 1, 2 6, 3 7 10, … | Learning and interviews |
| User-input rows | (int) trim(fgets(STDIN)); | Flexible console programs |
| Custom initial step | $m = 3 instead of rows - 1 | Tighter or wider jumps |
| Goal | Pattern |
|---|---|
| Walk each row | for ($i = 1; $i <= $rows; $i++) |
| Print row start | echo $i . " "; |
| Init step | int m = rows - 1; |
| First jump value | int k = i + m; |
| Inner loop | for ($j = 1; $j < $i; $j++) |
| Update step | m--; k = k + m; after each print |
| User input | (int) trim(fgets(STDIN)); |
Same jump triangle — different ways to control rows and step size.
print iEvery row begins with the row index
m = rows-1Initial jump size — reset each row
$m = 3Override step in Example 3
$m--Decrease m after each k print — jumps shrink
Reach for this pattern when teaching variable step sizes and computed sequences inside nested loops.
Natural follow-up after Program 20 — introduces a decreasing step variable.
Outer/inner bound practice with an immediate visual check.
Combine loops with fgets(STDIN) for a flexible row count.
Compare Program 20 (continuous counter) and Program 22 (odd-length rows) next.
This is a console teaching pattern — not how you build modern app screens.
Key benefit: one small program that locks in nested loops, output sequencing, and O(n²) thinking.
Choose a row count between 1 and 15 and draw the jump number triangle in the browser.
Three complete PHP programs — fixed row count, user input, and custom initial step for m. Click View Output to reveal sample console results.
Print five rows of the jump number triangle with a decreasing step.
rows = 5Hard-coded height — ideal for first demos and screenshots.
<?php
for ($i = 1; $i <= 5; $i++) {
echo $i . " ";
$m = 4;
$k = $i + $m;
for ($j = 1; $j < $i; $j++) {
echo $k . " ";
$m--;
$k = $k + $m;
}
echo PHP_EOL;
} When i = 2, print 2, then $m = 4 and k = 6 — the inner loop prints 6 once. When i = 3, print 3, then k = 7, $m-- to 3, k = 10 — output 3 7 10. echo PHP_EOL after the inner loop starts the next row.
Read the row count with fgets(STDIN) instead of hard-coding 5.
Read rows with (int) trim(fgets(STDIN)); set $m = $rows - 1 each row.
<?php
echo "Enter the number of rows: ";
$rows = (int) trim(fgets(STDIN));
for ($i = 1; $i <= $rows; $i++) {
echo $i . " ";
$m = $rows - 1;
$k = $i + $m;
for ($j = 1; $j < $i; $j++) {
echo $k . " ";
$m--;
$k = $k + $m;
}
echo PHP_EOL;
} Same nested-loop core as Example 1; only the source of rows changes. $m = $rows - 1 scales the initial jump with triangle height. Non-numeric input leaves rows unset if you skip is_numeric() checks — always validate in safer labs.
Use a fixed initial step instead of rows - 1.
m = 3Keep rows = 4 but start each row with $m = 3 for tighter jumps.
<?php
$rows = 4;
for ($i = 1; $i <= $rows; $i++) {
echo $i . " ";
$m = 3;
$k = $i + $m;
for ($j = 1; $j < $i; $j++) {
echo $k . " ";
$m--;
$k = $k + $m;
}
echo PHP_EOL;
} Change only the initial value of m — the inner loop and $k = $k + $m logic stay the same. Smaller starting steps produce tighter jumps within each row.
Set $rows = 5; and use fgets(STDIN) when reading input. Set loop variables $i, $j, $k, $m.
for ($i = 1; $i <= $rows; $i++) then echo $i . " " — row i prints i numbers.
$m = $rows - 1 and $k = $i + $m set up the first jump in that row.
Print $k, then $m-- and $k = $k + $m to compute the next jump.
echo PHP_EOL ends the row so the next outer iteration starts fresh.
Total prints: rows(rows+1)/2 — O(n²) time, O(1) extra memory.
rows = 5Trace each outer-loop value of i, the initial m, and the numbers printed on each row.
i | Init m | Jump sequence | Row output |
|---|---|---|---|
1 | 4 | 1 (no inner loop) | 1 |
2 | 4 → k=6 | 2, 6 | 2 6 |
3 | 4 → k=7, m=3 → k=10 | 3, 7, 10 | 3 7 10 |
4 | 4 → 8, 11, 13 | 4, 8, 11, 13 | 4 8 11 13 |
5 | 4 → 9, 12, 14, 15 | 5, 9, 12, 14, 15 | 5 9 12 14 15 |
Total number prints: 1 + 2 + 3 + 4 + 5 = 15 = 5×6/2.
Where this tiny pattern (and its loop structure) shows up beyond the homework prompt.
Clearest visual proof that outer and inner bounds interact.
Example: change j <= i and watch the shape change.
Foundation for inverted, pyramid, diamond, and hollow variants.
Example: use (i + j) % 2 for row+column parity grids.
Practice echo vs row newline without complex math.
Example: put echo PHP_EOL inside the inner loop by mistake.
Swap digits for letters, stars, or spaced output once the loop works.
Example: print j + " " for spaced digits on each row.
Triangular totals make O(n²) concrete for beginners.
Example: count printed digits for n = 10 still → 55.
Pair the pattern with fgets(STDIN) return checks and positive-row checks.
Example: reject rows <= 0 and re-prompt.
Pro Tip: when an interviewer asks for patterns, explain the outer/inner roles first — then write the loops. The story matters as much as the code.
Why this pattern earns a permanent spot in beginner C courses.
Wrong bounds show up immediately as a broken staircase.
Only loops and console output — no arrays or math libraries.
Invert, center, hollow, or change the fill character with small edits.
Streaming output needs no storage beyond loop counters.
Pro Tip: learn $m = $rows - 1 and $k = $i + $m first; compare with custom step in Example 3.
Small habits that keep number-pattern code clean.
Reset $m = $rows - 1 at the start of each row — not once before all loops.
is_numeric()Call is_numeric(trim($line)) so bad input does not leave rows uninitialized.
Only call echo PHP_EOL after the inner loop finishes the row.
Write row i, initial m, and each k jump before coding.
Trace rows = 5 on paper before coding larger demos.
Pro Tip: if the output is a vertical list of single digits per line, you almost certainly put echo PHP_EOL inside the inner loop.
Mistakes that commonly break jump number patterns.
Each digit lands on its own line — you get a column, not a triangle.
→ Use echo $i . " " and echo $k . " "; echo PHP_EOL only after the inner loop.
Using j <= i prints one extra value per row.
→ Use for ($j = 1; $j < $i; $j++) — only i - 1 jumps after printing i.
Skipping $m-- makes every jump the same size.
→ Always do $m-- then $k = $k + $m after printing k.
Omitting echo PHP_EOL glues every number onto one endless line.
→ Always end the row after the inner loop.
Letters or empty input leave rows uninitialized.
→ Prefer is_numeric(trim($line)) and re-prompt on failure.
Check these inputs before calling the solution done.
Output is just 1 on one line.
Outer loop never runs — print nothing or show a message.
rows < 0Treat as invalid; re-prompt instead of silent empty output.
Output grows as n² characters — fine for labs, noisy for huge n.
Unchecked fgets(STDIN) leaves rows unset — call is_numeric(trim($line)) first.
Declaring m once before all loops gives wrong jumps — reset inside each row.
When i = 1, the inner loop runs zero times — only 1 prints.
Try these variations to lock in the pattern.
k++ across rowsm = 2 or m = 6 instead of rows - 1rows(rows+1)/2 — O(n²) for n rows.echo $k . " " stays on the line; echo PHP_EOL advances — mix them carefully.rows > 0 for interactive programs; rows = 1 should print a single 1.Quick Takeaway: print i first, set $m = $rows - 1 and $k = $i + $m, then loop with $m-- and $k = $k + $m.
| Program | Time | Extra space |
|---|---|---|
| Nested loops (Examples 1–2) | O(rows²) | O(1) |
| Custom step (Example 3) | O(rows²) | O(1) |
The increasing jump number triangle is a compact lesson in variable step sizes: print i, set $m = $rows - 1, compute jumps with $k = $i + $m, and shrink m after each print. Master the fixed-rows version, then try user input and a custom step value.
Practice the three examples above, then continue to Program 22 for odd-length number rows.
Reset m each row — use j < i for the inner loop and validate rows when reading input.
i before the inner loop on every row$m = $rows - 1 inside each outer iterationfor ($j = 1; $j < $i; $j++) for jump valuesis_numeric(trim($line)) before using rowsecho PHP_EOL inside the inner jump loopj <= i — that prints one extra value$m-- before updating krows = 1 edge casePrint the pattern the beginner-friendly way.
Jump + m--
DefinitionRow start first
Coderows - 1, then m--
CodeRow i prints i nums
ShapeO(n²) time
AnalysisEach row starts at $i, then adds a decreasing step $m to compute the next value. As $m shrinks after each print, the jumps get smaller toward the end of the row — total prints still equal n(n+1)/2 for n rows.
Move on to the odd-length number rows pattern in the PHP number-pattern series.
12 people found this page helpful