Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Magic Square (Python3) A n x n matrix that is filled with the numbers 1, 2, 3, ..., n is a magic square if the

Magic Square (Python3) 

A n 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 print_square(). In the function print_square() you MUST use print with formatting.

You will then call the function check_square(). 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:

Please 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 

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Deductive And Object Oriented Databases Second International Conference Dood 91 Munich Germany December 18 1991 Proceedings Lncs 566

Authors: Claude Delobel ,Michael Kifer ,Yoshifumi Masunaga

1st Edition

3540550151, 978-3540550150

More Books

Students also viewed these Databases questions

Question

What is a report? What are they used for?

Answered: 1 week ago

Question

Does it avoid using personal pronouns (such as I and me)?

Answered: 1 week ago

Question

Does it clearly identify what you have done and accomplished?

Answered: 1 week ago