There is no "equation." But there are various and sundry methods. The one linked above on the 3x3 squares generates the only 3x3 square there is. (There are three others, but are the same square, just rotated around.)

Yes, I see a formula, lets try a 5x5 though so you could see the pattern
Fill 25 squares with numbers between 1 to 25

Rule: total of numbers from any side will be equal to 65.

Start with 1 in the middle top row. Then 2 should be one column to the right and one row up from 1, since there are no spot there, it goes to the way bottom of the column next right column from 1. Then 3 should be one column to the right and one row up. Then 4 should be one column to the right and one row up, since there are no spot there, it goes to the way left of the next top row where the 3 is at. Then 5 should be one column to the right and one row up from 4. Then 6 should be one column to the right and one row up from 5, but since 1 is there, it go below 5. etc