Back to Patterns
Numeric Spiral Pattern
Pattern Result
1 2 3 4 516 17 18 19 615 24 25 20 714 23 22 21 813 12 11 10 9
Code Implementation
Explanation
The Numeric Spiral Pattern creates a square matrix filled with consecutive numbers in a spiral order. The algorithm starts from the top-left corner and fills the matrix in a clockwise spiral pattern. It uses four pointers (rowStart, rowEnd, colStart, colEnd) to keep track of the boundaries of the unfilled portion of the matrix. In each iteration, it fills the top row, right column, bottom row, and left column of the current boundary, and then shrinks the boundary. The process continues until the entire matrix is filled. Finally, the matrix is converted to a pattern by joining each row into a string with spaces between the numbers.