A diagonal-fill number triangle starts each row at index $i, then builds later values with $res = $res + $k where $k begins at $rows − 1 and shrinks.
Remember
Rule: for i = 1..rows
start with i; k = rows - 1
next values: res = res + k; k--
1
2 6
3 7 10
4 8 11 13
5 9 12 14 15 ← rows = 5
Unlike Program 54 (space-filled X diamond), here every cell holds a number — filled as if walking down diagonals of a triangle. Program 56 continues with a palindromic number pyramid.
Approach
How to Solve It
Print $i first on each row; for later columns add a decreasing step $k. End each row with echo PHP_EOL.
Method
Idea
Best for
Fixed rows
Hard-code size; $res + $k steps
Labs and demos
CLI input
Same logic; read $rows at runtime
Interactive practice
Compact rows
implode without trailing space
Cleaner console output
Pseudocode
Pseudocode
for i from 1 to rows:
k = rows - 1
res = i
for j from i to i+i-1: // i values
if i == j: print i and a space
else:
res = res + k
print res and a space
k = k - 1
print newline
Cheat sheet
Goal
Pattern
Set size
$rows = 5;
Reset step
$k = $rows - 1; each row
First value
if ($i == $j) echo $j . " ";
Next value
$res = $res + $k; echo $res . " "; $k--;
Inner span
for ($j = $i; $j < $i + $i; $j++)
End the row
echo PHP_EOL;
Printing Numbers vs Starting a New Line
API
Effect
Use for
echo $j . " " / echo $res . " "
Stays on the same line
Each number in the row
echo PHP_EOL
Ends the current line
After the inner loop finishes
Print values side by side, then break once per row.
Try it
Live Preview
Change the row count and the diagonal-fill triangle updates instantly.
Whole numbers from 3 to 8. Tap a chip or type a value — the preview redraws as you go.
Live resultrows = 5 · values = 15
1
2 6
3 7 10
4 8 11 13
5 9 12 14 15
Trace
Worked Walkthrough
Trace $rows = 5, row $i = 3 — how $res and $k build 3 7 10.
Step
$j
Action
Print
1
3
$i == $j → print start
3
2
4
$res = 3 + 4; $k → 3
7
3
5
$res = 7 + 3; $k → 2
10
Full row: 3 7 10. Each new row resets $k to $rows − 1 (here 4).
Code
PHP Programs
Three complete programs: fixed $rows = 5, CLI input, and compact implode rows. Use View Output for sample results.
Example 1 — Fixed $rows = 5
Hard-coded size — each row uses $res + $k stepping with $k = $rows - 1.