I'm trying to solve this one assignment we have in PHP.

Based on the given number n, the script is supposed to print out a n*n grid of numbers following the pattern in this image:

In this example the given number n is 4.

Would any of you have an idea for a good algorithm?