Full Concentric Number Square in Python

What You’ll Learn
How to print the full concentric number square in Python. The outermost layer is 5, the next is 4, then 3, 2, and the center becomes 1. The pattern then expands back out symmetrically.
This is a common pattern problem that improves your understanding of layer-based grid logic.
⭐ Pattern Output
For max = 5, the pattern looks like this:
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
5 4 3 2 2 2 3 4 5
5 4 3 3 3 3 3 4 5
5 4 4 4 4 4 4 4 5
5 5 5 5 5 5 5 5 5Complete Python Program
This version follows the reference logic and prints the top half (5 down to 1) and then the bottom half (2 up to 5) to complete the full concentric square.
max_val = 5
# top half (5 down to 1)
for i in range(max_val, 0, -1):
for j in range(max_val, 1, -1):
print(j if j > i else i, end=" ")
for j in range(1, max_val + 1):
print(j if j > i else i, end=" ")
print()
# bottom half (2 up to 5)
for i in range(2, max_val + 1):
for j in range(max_val, 1, -1):
print(j if j > i else i, end=" ")
for j in range(1, max_val + 1):
print(j if j > i else i, end=" ")
print()🧠 How It Works
Pick the maximum layer
max_val = 5 is the outermost layer value.
Print the top half
The first outer loop runs i from 5 down to 1, creating increasingly smaller inner layers.
Mirror for the bottom half
The second outer loop runs i from 2 up to 5 to mirror the pattern and complete the square.
Cell value rule
For each printed position, we output max(i, j)-style layer logic (implemented as j if j > i else i) to build layers.
Concentric layers
We print a fixed-size grid and compute each cell, so runtime is O(size²).
Variation — Fully General (User Input)
This clean formula-based method prints the full \((2n-1)\times(2n-1)\) concentric square for any n.
n = int(input("Enter max value (n >= 1): "))
if n < 1:
raise ValueError("n must be at least 1")
size = 2 * n - 1
for i in range(size):
for j in range(size):
dist = min(i, j, size - 1 - i, size - 1 - j)
print(n - dist, end=" ")
print()💡 Tips for Enhancement
Try These
- Use fixed-width formatting to keep columns aligned for
n > 9 - Replace numbers with letters to create alphabet layers
- Print without spaces for a compact look
- Generate a rectangular layered pattern by changing the distance formula
- Try printing the inverse pattern (center max, border min)
Avoid
- Hardcoding the size when you want the pattern scalable
- Using
n < 1without validation - Mixing index bases in formulas
- Printing variable-width numbers without alignment
Key Takeaways
A full concentric square has size 2n-1.
Each cell belongs to a layer based on its distance to the nearest edge.
Layer values decrease as you move toward the center.
Time complexity is O(size²) for the grid.
❓ Frequently Asked Questions
(2n-1) square.Explore More Python Number Patterns!
Layered grids are a great stepping stone to spiral and maze patterns.
Cells with the same minimum distance to the border form a perfect ring (layer). That’s why the min()-distance trick generates concentric patterns so neatly.
7 people found this page helpful
