How do I create a magic square matrix using python

2024/11/15 6:06:48

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 / 2j = n - 1num = 1while num <= (n * n): if i == -1 and j == n:j = n - 2i = 0else:if j == n: j = 0 if i < 0: i = n - 1if m[int(i)][int(j)]:j = j - 2i = i + 1continueelse: m[int(i)][int(j)] = num num = num + 1j = j + 1i = i - 1print ("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)

`

Answer
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 - 2i = 0else:if j == n: j = 0 if i < 0: i = n - 1if m[int(i)][int(j)]:j = j - 2i = i + 1continueelse: m[int(i)][int(j)] = num num = num + 1j = j + 1i = 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)

https://en.xdnf.cn/q/119736.html

Related Q&A

Ensuring same dimensions in Python

The dimensions of P is (2,3,3). But the dimensions of M is (3,3). How can I ensure that both P and M have the same dimensions i.e. (2,3,3). import numpy as np P=np.array([[[128.22918457, 168.52413295,…

how to stop tkinter timer function when i press button one more times?

id tried to use root.after_cancel(AFTER), but i dont know how.root.after_cancel(AFTER) AFTER = None def countdown(count,time,name):global AFTERtime[text] =name,":",datetime.fromtimestamp(cou…

Read csv into database SQLite3 ODO Python

I am trying to read in a csv into a new table in a new databased using ODO, SQLite3 and Python.I am following these guides:https://media.readthedocs.org/pdf/odo/latest/odo.pdf http://odo.pydata.org/en/…

netmiko cant execute sh run | i host

I notice that my netmiko code cant run sh run | i host which is a legitimate Cisco command.When I replace sh run with other command such as sh clo, or show ip interface brief, it works perfectly.from n…

How to dump the data from file to an excel sheet

I want to dump [3-4 lines together] some data to an excel sheet. I could able to dump single line based on some criteria [like if line is getting start with // or /* ], but in case of when lines starts…

I dont understand why my script is not iterating through all string.split elements?

The objective of this python exercise is to build a function that turns text into pig latin, a simple text transformation that modifies each word by moving the first character to the end and appending …

Unable to change the tick frequency on my chart

I have seen many questions on changing the tick frequency on SO, and that did help when I am building a line chart, but I have been struggling when its a bar chart. So below are my codes import numpy a…

Django Queryset foreign keys

I am trying to get a queryset but it is not displaying anything. Basically, I want to get the Asset objects that are assigned via foreign key to an employee, which is a foreign key of the signed in use…

How to reorder the columns of a CSV?

How can I re-order the columns of a CSV file using Python? These are the first rows of a CSV file I need to change:03;30269714;Ramiro Alberto;Nederz;active;pgc_gral 03;36185520;Andrea;Espare;active;pg…

Distance matrix in Python Pandas

I am a newbie in python, but I like to process data in pandas. I have a hundred pairs of CSV data such as passenger and bus stop data. The passenger structure data is Person, and XY coordinates (UTM-Me…