# What kind of algorithm do i need when the place of numbers changing according to n number?

I have a project in Golang. But i don't have any idea about how to solve it.

n will be an odd number (Feedback will be given if an odd number is not written)

• As output, a structure with n*n matrix appearance will be created according to the following rules and printed on the screen.

## Rules

The rules will be transferred assuming the entered number is 3. (Any number can be entered)

• The first number will be in the middle column of the first row.
• The next number is placed in the diagonal square, to the top right.
• In such a case, the overflowing number is placed at the bottom of the column in the section it is in.
• The next number is placed in the diagonal square, to the top right.
• If the number overflows from the right, it is placed on the leftmost row.
• The next number is placed in the diagonal square, to the top right.
• If the cell is full, it is placed under the previously placed number. The number 4 is placed under the number 3.
• The next number, the number 5, is placed in the diagonal square to the top right.
• The next number, the number 6, is placed in the diagonal square to the top right.
• The next number, 7, is placed in the diagonal square to the top right.
• In case of cross overflow, it is placed below the previous number.
• For other numbers, the above rules are applied.
• Since the number 8 overflows from the right, it is placed on the far left in the line it is in.
• The number 9 overflows.
• In case of overflow, it is placed at the bottom of the column.
• All numbers are placed by operating the rules.

here's the output for n = 3

Enter the number : 3
8 1 6
3 5 7
4 9 2  I can make the zigzag matrix but I can't build an algorithm for this question. Can anyone help me about building the algorithm?

Progress:

I noticed that the sums of each row and column are equal. For example:

• n=3 ==> the sum of each row and column of the resulting matrix is equal to each other and to 15.
• n=5 ==> the sum of each row and column of the resulting matrix is equal to each other and 65.
• n=7 ==> the sum of each row and column of the resulting matrix is equal to each other and 175.
• n=9 ==> the sum of each row and column of the resulting matrix is equal to each other and 369.
• The first four rules pretty much spell it out: What more of a specification of well-defined steps to populate an n × n square do you need? Does Golang pose obstacles? Apr 26, 2022 at 7:09

Your observations to the sum of rows and columns hold the hint. Its a magic square !

There are numerous articles that go in depth into the construction of these squares and how they hold the properties they have. One of them here.

Now, the algorithm is just understanding these properties and then scaling them out to bigger N. The article I linked above shows a way to create a 9x9 square by treating it as a set of 9 3x3 squares.

package main

import (
"fmt"
)

func array(n int) (int, []int) {

m := make([]int, n*n)
A := func(x int) int { return (x + n - 1) % n }
i, j := n/2, 0
for k := 1; k <= n*n; k++ {
m[i*n+j] = k
if m[A(i)*n+A(j)] != 0 {
j = (j + 1) % n
} else {
i, j = A(i), A(j)
}
}
return n, m
}

func main() {
var n int
fmt.Print("Enter the n number: ")
fmt.Scanf("%d", &n)

if n%2 == 0 {
} else {
fmt.Println("n number: ", n)
n, m := array(n)
j := 2
for i := 1; i <= n*n; i *= 10 {
j++
}
f := fmt.Sprintf("%%%dd", j)
for j := 0; j < n; j++ {
for i := 0; i < n; i++ {
fmt.Printf(f, m[i*n+j])
}
fmt.Println()
}
}

}



here's the golang code for building n*n matrix (n is a number from user)which is a magic square