Full Concentric Number Square in Python

Beginner
⏱️ 7 min read
📚 Updated: Aug 2025
🎯 2 Code Examples
Grid Logic

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:

Output
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 5
1

Complete 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.

Python
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

1

Pick the maximum layer

max_val = 5 is the outermost layer value.

Setup
2

Print the top half

The first outer loop runs i from 5 down to 1, creating increasingly smaller inner layers.

Top half
3

Mirror for the bottom half

The second outer loop runs i from 2 up to 5 to mirror the pattern and complete the square.

Bottom half
4

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.

Layer rule
=

Concentric layers

We print a fixed-size grid and compute each cell, so runtime is O(size²).

2

Variation — Fully General (User Input)

This clean formula-based method prints the full \((2n-1)\times(2n-1)\) concentric square for any n.

Python
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 < 1 without validation
  • Mixing index bases in formulas
  • Printing variable-width numbers without alignment

Key Takeaways

1

A full concentric square has size 2n-1.

2

Each cell belongs to a layer based on its distance to the nearest edge.

3

Layer values decrease as you move toward the center.

4

Time complexity is O(size²) for the grid.

❓ Frequently Asked Questions

It represents the layer number at that position. The border is the maximum, and values decrease toward the center.
Yes. Use the variation that computes distance to edges and prints a (2n-1) square.
Because the nearest edge decides the layer. The smallest distance to any edge gives the layer index.
O(size²), because we compute and print every cell in the grid.

Explore More Python Number Patterns!

Layered grids are a great stepping stone to spiral and maze patterns.

All Number Patterns →
Did you know?

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.

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

7 people found this page helpful