An increasing jump number triangle starts each row at the row index, then jumps forward with a step that shrinks after every print.
Remember
Rule: print i, then add shrinking m (start m = n-1)
1
2 6
3 7 10
4 8 11 13
5 9 12 14 15 ← 5 rows
In PHP 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. Program 22 continues with odd-length number rows.
Approach
How to Solve It
Outer loop from 1 to $rows. Print $i, seed $m and $k, then print $i - 1 more jumped values. End each row with echo PHP_EOL.
Method
Idea
Best for
Decreasing step
Start at $i; add $m, then $m--
Learning, interviews, exams
Custom step
Seed $m with a fixed value instead of $rows - 1
Tighter or wider jumps
Pseudocode
Pseudocode
for i from 1 to rows:
print i and a space
m = rows - 1
k = i + m
for j from 1 to i - 1:
print k and a space
m = m - 1
k = k + m
print newline
Cheat sheet
Goal
Pattern
Pick row start
for ($i = 1; $i <= $rows; $i++)
Print row head
echo $i . " ";
Seed step & next
$m = $rows - 1; $k = $i + $m;
Jump loop
for ($j = 1; $j < $i; $j++) then $m--; $k = $k + $m;
End the row
echo PHP_EOL;
Custom step
$m = 3; instead of $rows - 1
Printing Numbers vs Starting a New Line
API
Effect
Use for
echo $i . " " / echo $k . " "
Stays on the same line
Each number in the row
echo PHP_EOL
Ends the current line
After the jump loop
Print all numbers without a newline, then end the row once.
Try it
Live Preview
Change the row count and the jump triangle updates instantly.
Whole numbers from 1 to 12. Tap a chip or type a value — the preview redraws as you go.
Live result5 rows · 15 numbers
1
2 6
3 7 10
4 8 11 13
5 9 12 14 15
Trace
Worked Walkthrough
Trace three rows when $rows = 5 ($m starts at 4 each row) — watch the jumps shrink.
$i
First jumps
Printed row
1
(none)
1
2
2 + 4 = 6
2 6
3
3+4=7, then 7+3=10
3 7 10
4
4+4=8, 8+3=11, 11+2=13
4 8 11 13
Total numbers are 1+2+3+4+5 = 15 — the triangular number n(n+1)/2.
Code
PHP Programs
Three complete programs: fixed height, CLI input, and a custom initial step. Use View Output for sample results.
Example 1 — Fixed $rows = 5
Print $i, then jump with $m = 4 shrinking after each value.
1. Same loop shape. Outer and inner structure match Example 1.
2. Different seed.$m = 3 replaces $rows - 1 (which would be 4).
3. Tighter jumps. Smaller starting steps pull later values closer together — compare row 2: 2 5 here vs 2 6 in Example 1.
Edge Cases & Pitfalls
Check these before calling the solution done.
forget m--
Forgetting $m--
Every jump stays the same size. Decrease $m after each print so later values tighten.
wrong bound
$j <= $i instead of $j < $i
That prints one extra jumped value. The inner loop runs $i - 1 times after the head.
PHP_EOL inside
echo PHP_EOL inside the jump loop
That puts each number on its own line. Call echo PHP_EOL only after the row finishes.
rows = 1
Single number
Output is just 1 — the jump loop never runs.
rows ≤ 0
Empty output
The outer loop never runs. Validate and prompt again for clearer UX.
spaces
Keep the trailing space
The classic sample uses echo $i . " " / echo $k . " " so numbers stay spaced and readable.
Analysis
Time and Space Complexity
Program
Time
Extra space
Fixed / input / custom step (Examples 1–3)
O(n²)
O(1)
There are n rows printing 1+2+…+n numbers, so total work is n(n+1)/2.
Remember
Key Takeaways
Rule: print $i, then jump with a shrinking $m that starts at $rows - 1.
Update order: print $k, then $m--, then $k = $k + $m.
echo vs PHP_EOL: numbers stay on the line; echo PHP_EOL advances after each row.
Next step: Program 22 prints odd-length number rows.
One line: print $i, then add a shrinking $m to build the rest of the row.
Frequently Asked Questions
Because the step variable $m starts at $rows - 1 and decreases after each printed number. Each next value is computed by adding the current $m, so the jumps shrink across the row.
$m is the step size used to compute the next printed number in the row. It decreases each time, changing the increment between consecutive outputs.
With rows = 5, row 2 prints $i = 2 first. Then $m = 4 and $k = $i + $m = 6 — the inner loop prints $k once, giving 2 6.
echo $i . " " stays on the same line with a trailing space. echo PHP_EOL ends the current line. Numbers use echo; the row break uses echo PHP_EOL after the inner loop.
Total prints are 1+2+...+n = n(n+1)/2 — row i prints exactly i values.
Yes. Set $m to a custom value instead of $rows - 1 (see Example 3) — smaller steps produce tighter jumps.
O(n²) for n rows because total digit prints equal n(n+1)/2.
Use trim(fgets(STDIN)) and check is_numeric($input) before casting to int so bad input does not produce unexpected results.
The outer loop never runs, so nothing is printed. Validate and prompt again if you want a clear user message.
🤔
Did you know?
Each 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.