Row Start
$res = $i
First value on row $i is always $i when $i == $j.

The diagonal-fill triangle starts each row with index $i, then generates remaining values using $res = $res + $k where $k begins at $rows - 1 and decreases. For $rows = 5: 1, 2 6, 3 7 10, 4 8 11 13, 5 9 12 14 15. This tutorial covers the step logic, live preview, worked PHP examples, edge cases, and O(n²) complexity.
$res = $i
First value on row $i is always $i when $i == $j.
$k = $rows-1
$k starts at $rows-1 each row and decreases after each addition step.
$res + $k
After the first value, update $res = $res + $k and echo $res; then $k--.
i values
Row $i prints exactly $i numbers — inner loop $j = $i..$i+$i-1.
3–12 for n
Pick size n and draw the full diagonal-fill triangle pattern instantly in the browser.
Complexity
Total prints = n(n+1)/2 — triangular growth, O(n²) time.
A diagonal-fill number triangle starts each row with $i and builds the rest using decreasing step sizes. Row 2 becomes 2 6; row 3 becomes 3 7 10.
In PHP: outer for ($i = 1; $i <= $rows; $i++), set $k = $rows - 1 and $res = $i, inner for ($j = $i; $j < $i + $i; $j++) — echo $j when $i == $j, else $res = $res + $k, then echo PHP_EOL.
It shows how a running total and a shrinking step fill a triangle without a 2D array — the same numbers as a column-wise fill, generated row by row.
First value on row $i is $i when $i == $j.
$kStarts at $rows - 1 each row, then $k--.
After the first value: $res = $res + $k.
Follow Program 54; continue to Program 56 next.
In short: outer $i = 1..$rows; set $k = $rows - 1 and $res = $i; inner $j = $i..$i+$i-1 prints $j or $res + $k, then echo PHP_EOL.
Given $rows = 5, print five lines with 1, 2, 3, 4, and 5 numbers respectively.
// $rows = 5 (conceptual output)
// 1
// 2 6
// 3 7 10
// 4 8 11 13
// 5 9 12 14 15 | Item | Type | Description |
|---|---|---|
$rows | int | Number of triangle rows (typically ≥ 1). |
$i, $j, $k, $res | int | Row $i; inner index $j; step $k; running total $res. |
| Printed output | text | $i numbers on row $i; total $rows($rows+1)/2 values. |
for $i from 1 to $rows:
$k = $rows-1; $res = $i
for $j from $i to $i+$i-1:
if $i==$j: echo $j
else: $res = $res+$k; echo $res; $k--
echo PHP_EOL | Approach | Idea | Best for |
|---|---|---|
| $res + $k stepping | $res = $res + $k with $k decreasing each step | Diagonal-fill sequence within each row |
| implode row | Build row without trailing spaces | Cleaner console output — Example 3 |
| fgets(STDIN) input | (int) $input for rows | User-chosen row count |
| Compact output | implode joins values with single spaces | No trailing space per row — Example 3 |
| Goal | Pattern |
|---|---|
| Set rows | $rows = 5; |
| Outer loop | for ($i = 1; $i <= $rows; $i++) |
| Row setup | $k = $rows - 1; $res = $i; |
| Inner loop | for ($j = $i; $j < $i + $i; $j++) |
| First value | if ($i == $j) echo $j |
| Next values | $res = $res + $k; echo $res; $k--; |
| Row break | echo PHP_EOL; after each row |
| Program 54 contrast | Diamond diagonal uses fixed-width rows; this triangle grows i values per row |
How row start, step variable, inner bounds, and row breaks work together.
$res = $i
if ($i == $j) echo $jFirst number on row $i is always $i.
$k = $rows - 1
$res = $res + $k
$k--Each subsequent value adds a shrinking step size.
for ($j = $i; $j < $i + $i; $j++)Row $i prints exactly $i numbers.
trace $i=3, $rows=5Dry-run row 3: start 3, then +4→7, +3→10 → 3 7 10.
Reach for this pattern when teaching step variables, running totals, and growing row lengths.
Classic follow-up after diamond diagonal patterns — introduces step-based number generation.
Outer/inner bound practice with an immediate visual check.
Combine loops with fgets(STDIN) for a flexible pattern size.
Compare with Program 54 (fixed-width diamond), then continue to Program 56 palindromic pyramid.
This is a console teaching pattern — not how you build modern app screens.
Key benefit: one program that locks in step variables, running totals, and O(n²) thinking.
Choose pattern size n and draw the full diagonal-fill triangle number pattern in the browser.
Three complete PHP programs — fixed $rows = 5, fgets(STDIN) input, and a compact implode variant. Click View Output to reveal sample console results.
Print five rows — each row grows by one number using $res + $k stepping.
$rows = 5Hard-coded $rows = 5 — each row uses $res + $k stepping with $k = $rows - 1.
<?php
$rows = 5;
for ($i = 1; $i <= $rows; $i++) {
$k = $rows - 1;
$res = $i;
for ($j = $i; $j < $i + $i; $j++) {
if ($i == $j) {
echo $j . " ";
} else {
$res = $res + $k;
echo $res . " ";
$k--;
}
}
echo PHP_EOL;
} Row 1 prints just 1. Row 3 starts at 3, adds 4 to get 7, adds 3 to get 10 — output 3 7 10.
Read $rows with fgets(STDIN) for flexible output size.
Same stepping logic; row count comes from user input.
<?php
echo "Enter rows: ";
$input = trim(fgets(STDIN));
if (!is_numeric($input)) {
echo "Invalid input." . PHP_EOL;
exit(1);
}
$rows = (int) $input;
for ($i = 1; $i <= $rows; $i++) {
$k = $rows - 1;
$res = $i;
for ($j = $i; $j < $i + $i; $j++) {
if ($i == $j) echo $j . " ";
else {
$res = $res + $k;
echo $res . " ";
$k--;
}
}
echo PHP_EOL;
} Same stepping logic as Example 1; row count comes from fgets(STDIN) input.
Build each row with an array and implode — no trailing space.
Same logic; use an array and implode to join values without a trailing space.
<?php
$rows = 5;
for ($i = 1; $i <= $rows; $i++) {
$k = $rows - 1;
$res = $i;
$parts = [];
for ($j = $i; $j < $i + $i; $j++) {
if ($i == $j) $parts[] = $j;
else {
$res = $res + $k;
$parts[] = $res;
$k--;
}
}
echo implode(" ", $parts) . PHP_EOL;
} Same stepping logic; implode on an array produces clean rows without trailing spaces.
echo is built in; use fgets(STDIN) when reading input. Set $rows (e.g. 5).
for ($i = 1; $i <= $rows; $i++) — one triangle row per iteration.
Each row: $k = $rows - 1, $res = $i — step size and running total reset.
$j = $i..$i+$i-1: echo $j when $i==$j, else $res += $k and $k--; then echo PHP_EOL.
Total prints = n(n+1)/2 — O(n²) time, O(1) extra memory.
$i = 3, $rows = 5Trace row 3 to see how $res + $k stepping produces 3 7 10.
| Step | j | Action / row so far |
|---|---|---|
| Start | — | $k=4, $res=3 |
| First | 3 | $i==$j → echo 3 → 3 |
| Second | 4 | $res=3+4=7, $k=3 → 3 7 |
| Third | 5 | $res=7+3=10, $k=2 → 3 7 10 |
Final row 3 output: 3 7 10. Then echo PHP_EOL moves to row 4.
Where this tiny pattern (and its loop structure) shows up beyond the homework prompt.
Clearest visual proof that outer and inner bounds interact.
Example: use fgets(STDIN) for dynamic rows — see Example 2.
$k starts at $rows-1 and decreases — controls how far each step jumps.
Example: trace row 3 with rows=5 — 3, then +4→7, +3→10.
Practice echo PHP_EOL vs echo for multi-line vs same-line output.
Example: use implode for clean rows — see Example 3.
Last value on row n is always the triangular number n(n+1)/2.
Example: rows=5 ends with 15 — total count of printed numbers.
Total prints n(n+1)/2 makes O(n²) concrete for beginners.
Example: count values for rows=5 → 1+2+3+4+5 = 15 prints.
Pair the pattern with fgets(STDIN) and positive-row checks.
Example: reject $rows <= 0 and re-prompt.
Pro Tip: when an interviewer asks for patterns, explain the outer/inner loop roles first — then write the loops. The story matters as much as the code.
Why this pattern earns a permanent spot in beginner PHP courses.
Wrong step logic shows immediately — row values grow too fast or too slow.
Only loops and console output — no arrays or math libraries.
Change $rows, use fgets(STDIN), or join values with implode for compact rows.
Streaming output needs no storage beyond loop counters.
Pro Tip: learn the $res + $k stepping first; then try fgets(STDIN) input and the implode variant in Example 3.
Small habits that keep number-pattern code clean.
Use $rows for height and $i/$j/$k/$res for loop variables.
fgets(STDIN)Avoid crashes when the user types letters instead of a number.
Finish the inner loop for row $i, then call echo PHP_EOL.
Set $k = $rows - 1 and $res = $i at the start of every outer iteration.
Trace $rows = 5, $i = 3 on paper — expect 3 7 10.
Pro Tip: if row values grow too fast, check whether $k-- runs after each $res + $k step.
Mistakes that commonly break diagonal-fill triangle number patterns.
Each value lands on its own line — you get a column, not a triangle row.
→ Use echo inside the inner loop; echo PHP_EOL only after it finishes.
If $k never decreases, every step adds the same offset — row values grow too fast.
→ Call $k-- after each $res = $res + $k update.
Omitting echo PHP_EOL after the inner loop glues all rows onto one line.
→ Always call echo PHP_EOL after the inner loop completes.
$k must reset to $rows - 1 at the start of each outer row, not once globally.
→ Place $k = $rows - 1 inside the outer loop, before the inner loop.
Letters or empty input throw invalid input without validation.
→ Use is_numeric($input) before (int) $input.
Using literal 5 in loop bounds instead of variable $rows breaks dynamic input.
→ Use one $rows variable for outer loop and $k initialization.
Check these inputs before calling the solution done.
Output is one line: 1 — the inner loop prints a single value.
Loop never runs — print nothing or show a message.
$rows < 0Treat as invalid; re-prompt instead of silent empty output.
Large values produce wide rows — fine for labs; use smaller n for quick demos.
Unchecked fgets(STDIN) input leaves $rows unset — call is_numeric($input) first.
Use conditional spacing to avoid trailing spaces — see Example 3.
Try these variations to lock in the pattern.
$rows = 3, 6, or 8i prints exactly i numbers$k = $rows instead of $rows - 1$rows($rows+1)/2 (e.g. 15 numbers for $rows=5).echo stays on the line; echo PHP_EOL advances — mix them carefully.$rows > 0 for interactive programs; $rows = 1 prints one number.if ($i==$j) echo $j; else $res = $res + $k; echo $res; $k--;.Quick Takeaway: outer $i=1..$rows, $k=$rows-1, $res=$i; inner $j=$i..$i+$i-1; step with $res+$k; then echo PHP_EOL.
| Program | Time | Extra space |
|---|---|---|
Fixed $rows = 5 (Example 1) | O(n²) | O(1) |
| fgets(STDIN) input (Example 2) | O(n²) | O(1) |
| Compact rows (Example 3) | O(n²) | O(1) |
The diagonal-fill triangle combines a growing inner loop with decreasing step sizes — a natural step after fixed-width diamond patterns. Master the fixed-$rows version first, then try fgets(STDIN) input and the compact implode variant in Example 3.
Practice the three examples above, then continue to Program 56 for the palindromic number pyramid pattern.
Reset $k = $rows - 1 and $res = $i at the start of every row — one echo PHP_EOL per outer iteration.
$i, $res + $k stepping, and inner bounds before coding$k = $rows - 1 and $res = $i at the start of each row; then echo PHP_EOL$rows ≥ 1 for interactive programsfgets(STDIN) return value before using $rows$k = $rows - 1 each rowk-- after each step$rows$rows = 1 edge caseStart each row at $i, then add shrinking steps via $res + $k.
Row $i prints $i numbers
$i = 1..$rows
Code$k starts at $rows-1
Logic$i values per row
O(n²)
AnalysisEach row starts with index $i, then adds decreasing step sizes via $res = $res + $k where $k starts at $rows - 1. Row $i prints exactly $i numbers.
Move on to the palindromic number pyramid pattern in the PHP number-pattern series.
12 people found this page helpful