-1

A basket is given to you in the shape of a matrix. If the size of the matrix is N x N then the range of number of eggs you can put in each slot of the basket is 1 to N2 . You task is to arrange the eggs in the basket such that the sum of each row, column and the diagonal of the matrix remain same

This code is working only for odd numbers but not even numbers.

here's my code that i tried but it didn't work `

def matrix(n): 
    m = [[0 for x in range(n)] 
                      for y in range(n)]
    i = n / 2
    j = n - 1
    num = 1
    while num <= (n * n): 
        if i == -1 and j == n:
            j = n - 2
            i = 0
        else:
            if j == n: 
                j = 0 
            if i < 0: 
                i = n - 1
        if m[int(i)][int(j)]:
            j = j - 2
            i = i + 1
            continue
        else: 
            m[int(i)][int(j)] = num 
            num = num + 1
        j = j + 1
        i = i - 1
    print ("Sum of eggs in each row or column and diagonal ",n * (n * n + 1) / 2, "\n") 
    for i in range(0, n): 
        for j in range(0, n): 
            print('%2d ' % (m[i][j]),end = '') 
            if j == n - 1:  
                print()
n=int(input("Number of rows of matrix:"))
matrix(n)

`

  • There is a simple formula for creating a magic square whose sides are odd, that doesn't work when the sides are even. You're apparently using that algorithm. A quick Google search should help you deal with even-sided squares, too. – Frank Yellin Nov 10 '22 at 19:24

2 Answers2

0
def matrix(n): 
m = [[0 for x in range(n)] 
                  for y in range(n)]
i = n / 2
j = n - 1
num = 1
while num <= (n * n): 
    if i == -1 and j == n:
        j = n - 2
        i = 0
    else:
        if j == n: 
            j = 0 
        if i < 0: 
            i = n - 1
    if m[int(i)][int(j)]:
        j = j - 2
        i = i + 1
        continue
    else: 
        m[int(i)][int(j)] = num 
        num = num + 1
    j = j + 1
    i = i - 1
print ("Sum of eggs in each row or column and diagonal ",n * (n * n + 1) / 2, "\n") 
for i in range(0, n): 
    for j in range(0, n): 
        print('%2d ' % (m[i][j]),end = '') 
        if j == n - 1:  
            print()

n=int(input("Number of rows of matrix:")) matrix(n)

0
def forEvenNumber(n):
    arr = [[(n * y) + x + 1 for x in range(n)] for y in range(n)]
    for i in range(0, n // 4):
        for j in range(0, n // 4):
            arr[i][j] = (n * n + 1) - arr[i][j];
    for i in range(0, n // 4):
        for j in range(3 * (n // 4), n):
            arr[i][j] = (n * n + 1) - arr[i][j];
    for i in range(3 * (n // 4), n):
        for j in range(0, n // 4):
            arr[i][j] = (n * n + 1) - arr[i][j];
    for i in range(3 * (n // 4), n):
        for j in range(3 * (n // 4), n):
            arr[i][j] = (n * n + 1) - arr[i][j];
    for i in range(n // 4, 3 * (n // 4)):
        for j in range(n // 4, 3 * (n // 4)):
            arr[i][j] = (n * n + 1) - arr[i][j];
    print("\nSum of all row, column and diagonals = ",
          n * (n * n + 1) // 2, "\n")
    for i in range(n):
        for j in range(n):
            print('%2d ' % (arr[i][j]), end=" ")
        print()
def forOddNumber(n):
    mgsqr = [[0 for x in range(n)]
             for y in range(n)]
    r = n // 2
    c = n - 1
    num = 1
    while num <= (n * n):
        if r == -1 and c == n:
            c = n - 2
            r = 0
        else:
            if c == n:
                c = 0
            if r < 0:
                r = n - 1
        if mgsqr[int(r)][int(c)]:
            c = c - 2
            r = r + 1
            continue
        else:
            mgsqr[int(r)][int(c)] = num
            num = num + 1
        c = c + 1
        r = r - 1
    print("\nSum of all row, column and diagonals = ",
          n * (n * n + 1) // 2, "\n")
    for i in range(0, n):
        for j in range(0, n):
            print('%2d ' % (mgsqr[i][j]), end='')
        print()
print("\nWELCOME:)\n")
n = int(input("Please Enter Number of Rows and Column (n*n): "))
if n%2==0:
    forEvenNumber(n)
else:
    forOddNumber(n)
print("\nThank You :)")

This should take in the even inputs and give the right outputs!

Benz_10
  • 1
  • 1
  • 1
    Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Dec 29 '22 at 14:23
  • I think the even version here only works for n a multiple of 4. – Andrew Steane Jun 04 '23 at 15:30