subject

An x n matrix that is filled with the numbers 1, 2, 3, n² is a magic square if the sum of the elements in each row, in each column, and in the two diagonals is the same value.
implement the following algorithm to construct the magic n-by-n squares. this algorithm works only if n is odd.
place a 1 in the middle of the bottom row.
after k has been placed in the (i, j) square, place k+1 into the square to the right and down, wrapping around the borders.
however, if the square to the right and down has already been filled, or if you are in the lower right corner, then you must move to the square straight up (from the last square that you were on) instead.
skeleton:
# populate a 2-d list with numbers from 1 to n2
def make_square ( n ):
# print the magic square in a neat format where the numbers
# are right justified
def print_square ( magic_square ):
# check that the 2-d list generated is indeed a magic square
def check_square ( magic_square ):
def main():
# prompt the user to enter an odd number 3 or greater
# check the user input
# create the magic square
# print the magic square
# verify that it is a magic square
main()
in your function main() you will prompt the user to enter an odd number. you must check that the input is a positive odd number greater than or equal to 3. if it is not, you will prompt the user to re-enter the number and check again and again.
then you will create a 2-d list representing the magic square. you will then print out the magic square in a neat format by calling the function in the function print_square() you must use print with formatting.
you will then call the function this function checks that the sum of all the rows have the same value and prints out that sum. it checks that the sum of all the columns have the same value and prints out that sum. it sums the two main diagonals and prints out the sum. for a magic square of size n, the sum is n * (n2 + 1) / 2.
this is a sample of what the program will output:
enter an odd number: 5
here is a 5 x 5 magic square:
11 18 25 2 9
10 12 19 21 3
4 6 13 20 22
23 5 7 14 16
17 24 1 8 15
sum of row = 65
sum of column = 65
sum diagonal (ul to lr) = 65
sum diagonal (ur to ll) = 65

ansver
Answers: 3

Another question on Computers and Technology

question
Computers and Technology, 22.06.2019 12:40
The most complicated four letter word
Answers: 1
question
Computers and Technology, 24.06.2019 00:00
Afashion designer wants to increase awareness about her brand. which network can she use and why she can use the blank to blank her products online. answers for the first blank: internet, extranet, or intranet answers for the second blank: market, design, and export
Answers: 1
question
Computers and Technology, 24.06.2019 22:00
Is the process of organizing data to reduce redundancy. a. normalization b. primary keying c. specifying relationships d. duplication
Answers: 1
question
Computers and Technology, 25.06.2019 07:50
In addition to the four primary computer operations, today's computers typically also perform functions.
Answers: 1
You know the right answer?
An x n matrix that is filled with the numbers 1, 2, 3, n² is a magic square if the sum of the eleme...
Questions
question
Arts, 21.05.2021 23:00
question
Mathematics, 21.05.2021 23:00
question
Mathematics, 21.05.2021 23:00