Shape Rule
max($i,$j)
Print j when $j > $i; otherwise print i — equivalent to max($i, $j).

The concentric number square prints symmetric rows where each value comes from max($i, $j). The left half walks $j = $k..1 and the right half mirrors $j = 2..$k. This tutorial covers the rule, nested loops, live preview, algorithm steps, worked PHP examples, edge cases, and complexity.
max($i,$j)
Print j when $j > $i; otherwise print i — equivalent to max($i, $j).
$j = $k..1
for ($j = $k; $j >= 1; $j--) builds the decreasing left side of each row.
$j = 2..$k
for ($j = 2; $j <= $k; $j++) mirrors the left half without duplicating the center.
$i = $k..1
for ($i = $k; $i >= 1; $i--) — each row gets closer to the center value.
$k = 3–10
Pick a value for $k and draw the concentric number square instantly in the browser.
Complexity
Prints k rows with about 2*$k-1 values each — O(k²) total prints; memory stays O(1).
A concentric number square pattern prints symmetric rows of numbers that decrease toward the center. With $k = 5, the last row becomes 5 4 3 2 1 2 3 4 5.
In PHP you loop $i = $k..1, then for each row print max($i, $j) over the left half ($j = $k..1) and right half ($j = 2..$k).
It teaches a reusable grid rule — once you spot max($i,$j), the nested loops become straightforward.
Each cell prints the larger of the row index and column value.
$j = $k..1 produces the decreasing left segment.
$j = 2..$k mirrors without repeating the center.
Follow Program 45 star cross; continue to Program 47 concentric diamond.
In short: loop $i = $k..1, print max($i,$j) for left and right halves, then echo PHP_EOL after each row.
Given $k = 5, print $k symmetric rows where each value equals max($i, $j) over mirrored column loops.
// $k = 5 (conceptual shape)
// 5 5 5 5 5 5 5 5 5
// 5 4 4 4 4 4 4 4 5
// 5 4 3 3 3 3 3 4 5
// 5 4 3 2 2 2 3 4 5
// 5 4 3 2 1 2 3 4 5 | Item | Type | Description |
|---|---|---|
$k | int | Maximum value and row count (typically ≥ 1). |
| Printed output | text | $k rows, each with 2*$k-1 space-separated numbers. |
for $i from $k down to 1:
for $j from $k down to 1:
print max($i, $j)
for $j from 2 to $k:
print max($i, $j)
print newline | Approach | Idea | Best for |
|---|---|---|
| if-else max rule | $j > $i ? $j : $i in both halves | Learning and interviews |
| User-input k | (int) $input; | Flexible console programs |
| max compact | max($i,$j) in both loops | Cleaner production-style code |
| Goal | Pattern |
|---|---|
| Walk rows | for ($i = $k; $i >= 1; $i--) |
| Left half | for ($j = $k; $j >= 1; $j--) |
| Right half | for ($j = 2; $j <= $k; $j++) |
| Value rule | $j > $i ? $j : $i or max($i, $j) |
| End the row | echo PHP_EOL; |
| Program 45 contrast | Star cross uses symbol conditions; this pattern uses max($i,$j) over mirrored number loops |
Same row cell — how max($i,$j) picks the printed number.
$j = $k..1Decreasing column walk — prints larger edge values first
$j = 2..$kMirrors the left segment without duplicating the center
max($i,$j)Print $j when $j > $i; otherwise print $i
trace $i=3,$j=2Dry-run cell (3,2): max(3,2) → prints 3
Reach for this pattern when teaching max($i,$j) inside mirrored column loops.
Classic follow-up after concentric squares and diamonds.
Outer/inner bound practice with an immediate visual check.
Combine loops with fgets(STDIN) for a flexible row count.
Compare with Program 45 (star cross), then continue to Program 47 (concentric diamond).
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(k²) thinking.
Choose a value for $k and draw the concentric number square in the browser.
Three complete PHP programs — fixed $k, fgets(STDIN) input, and a max compact variant. Click View Output to reveal sample console results.
Print five rows with mirrored inner loops and the max($i,$j) rule.
$k = 5Hard-coded size — left half $j=$k..1, right half $j=2..$k, max rule in each cell.
<?php
$k = 5;
for ($i = $k; $i >= 1; $i--) {
for ($j = $k; $j >= 1; $j--) {
if ($j > $i) {
echo $j . " ";
} else {
echo $i . " ";
}
}
for ($j = 2; $j <= $k; $j++) {
if ($j > $i) {
echo $j . " ";
} else {
echo $i . " ";
}
}
echo PHP_EOL;
} When $i = 3, $j = 2 on the left half, $j > $i is false — so the cell prints 3. When $i = 1 on the last row, the center column prints 1.
Let the user choose $k at runtime.
Read $k with trim(fgets(STDIN)) (check is_numeric($input) in real apps).
<?php
echo "Enter k (e.g., 5): ";
$input = trim(fgets(STDIN));
if (!is_numeric($input)) {
echo "Invalid input." . PHP_EOL;
exit(1);
}
$k = (int) $input;
for ($i = $k; $i >= 1; $i--) {
for ($j = $k; $j >= 1; $j--) {
echo ($j > $i ? $j : $i) . " ";
}
for ($j = 2; $j <= $k; $j++) {
echo ($j > $i ? $j : $i) . " ";
}
echo PHP_EOL;
} Same nested-loop core as Example 1; only the source of $k changes. Non-numeric input fails is_numeric($input) — validate before casting to int for safer labs.
Replace the if-else with max($i, $j) for cleaner code.
Use max($i, $j) in both inner loops — same output, less branching.
<?php
$k = 5;
for ($i = $k; $i >= 1; $i--) {
for ($j = $k; $j >= 1; $j--) {
echo max($i, $j) . " ";
}
for ($j = 2; $j <= $k; $j++) {
echo max($i, $j) . " ";
}
echo PHP_EOL;
} max($i, $j) expresses the same rule as $j > $i ? $j : $i — easier to read once you know the pattern math.
echo is built in; use fgets(STDIN) when reading input. Set $k (fixed or from input).
for ($i = $k; $i >= 1; $i--) — each row gets closer to the center value.
for ($j = $k; $j >= 1; $j--) prints the decreasing left segment of each row.
Both halves print max($i,$j) with a trailing space, then echo PHP_EOL ends the row.
Total cell visits equal k×(2*$k-1) — O(k²) time, O(1) extra memory.
$i = 3, left $j = 2Trace one left-half cell to see the max($i,$j) rule in action.
| Check | Result | Prints |
|---|---|---|
$j > $i (2>3) | false | print $i = 3 |
| Equivalent | max(3,2) | 3 |
| Row context | $i=3, $k=5 | middle row of five |
Cell output: 3 — full row has 2*$k-1 = 9 values when $k=5.
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 max($i,$j) instead of if-else — see Example 3.
Foundation for concentric layouts, symmetric grids, and distance-based rules.
Example: swap max for min(i,j) to explore a different shape.
Practice echo vs row newline without complex math.
Example: put echo PHP_EOL inside the inner loop by mistake.
Swap numbers for letters or stars once the max rule works.
Example: print row numbers with leading spaces for alignment.
Grid totals make O(k²) concrete for beginners.
Example: count values for $k=5 → 5 rows × 9 values = 45 prints.
Pair the pattern with fgets(STDIN) and positive-row checks.
Example: reject k <= 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 PHP courses.
Wrong mirror bounds (starting right half at 1) duplicate the center value.
Only loops and console output — no arrays or math libraries.
Change k, swap max for min, or mirror rows below for a full square.
Streaming output needs no storage beyond loop counters.
Pro Tip: learn the three-condition grid first; then try the max compact in Example 3.
Small habits that keep number-pattern code clean.
Use $k for the outer bound and keep i/j for row/column — or rename to row/col.
fgets(STDIN)Avoid crashes when the user types letters instead of a number.
Only call echo PHP_EOL after the inner loop finishes the row.
Compact: echo max($i, $j) . " "; in both inner loops inside the inner loop.
Trace $k = 3 on paper before coding larger demos.
Pro Tip: if the output is a vertical list of numbers per line, you almost certainly put echo PHP_EOL inside the inner loop.
Mistakes that commonly break concentric number square patterns.
Each number lands on its own line — you get a column, not a symmetric row.
→ Use echo for each value; echo PHP_EOL only after both inner loops finish.
Starting the right loop at $j = 1 prints the center twice on every row.
→ Keep for ($j = 2; $j <= $k; $j++) so the center appears once.
Omitting echo PHP_EOL glues every row onto one endless line.
→ Always end the row after both inner loops complete.
Letters or empty input leave $k unset.
→ Use is_numeric($input) and re-prompt on failure.
Using literal 5 in loop bounds instead of variable $k breaks dynamic input.
→ Use one $k variable for the outer bound and both inner loops.
Check these inputs before calling the solution done.
Output is one row of 2*$k-1 numbers; for k=1 you get a single 1.
Outer loop never runs — print nothing or show a message.
k < 0Treat as invalid; re-prompt instead of silent empty output.
Output grows with k rows and 2*$k-1 values per row — fine for labs, noisy for huge k.
Unchecked fgets(STDIN) leaves $k unset — call is_numeric($input) first.
Use max($i,$j) for cleaner code — see Example 3.
Try these variations to lock in the pattern.
$k = 3, 4, or 6StringBuilderk×(2*$k-1) (e.g. 5×9 = 45 for $k=5).echo stays on the line; echo PHP_EOL advances — mix them carefully.$k > 0 for interactive programs; $k = 1 prints one value.$j = 2 so the center value is not duplicated.Quick Takeaway: set $k, loop $i = $k..1, print max($i,$j) for left and right halves, then break the row.
| Program | Time | Extra space |
|---|---|---|
| Nested loops (Examples 1–2) | O(k²) | O(1) |
| max compact (Example 3) | O(k²) | O(1) |
The concentric number square pattern combines nested loops with a simple grid fill pattern — a natural step after concentric layouts. Master the fixed-$k version first, then try user input and the max compact form in Example 3.
Practice the three examples above, then continue to Program 47 for the concentric number diamond pattern.
Every cell uses print — keep echo PHP_EOL only after the inner column loop finishes.
print(max($i,$j) + " ") and echo PHP_EOL after both inner loops$k ≥ 1 for interactive programsfgets(STDIN) return value before using $kecho PHP_EOL between left and right halves (mid-row break)$j = 1 (duplicates center)$k$k = 1 edge casePrint the pattern the beginner-friendly way.
Each cell prints max($i,$j)
Definition$j = $k..1
Code$j = 2..$k
Logic2*$k-1
I/OO(k²)
AnalysisEach cell prints max($i, $j) — the left half loops $j = $k..1, the right half mirrors with $j = 2..$k. The smallest value appears at the center of the last row.
Move on to the concentric number diamond pattern in the PHP number-pattern series.
12 people found this page helpful