Question

In: Computer Science

PYTHON: Implement the following algorithm to construct magic n × n squares; it works only if...

PYTHON: Implement the following algorithm to construct magic n × n squares; it works only if n is odd.

row ← n - 1

column ← n/2

for k = 1 . . . n2 do

Place k at [row][column]

Increment row and column

if the row or column is n then

replace it with 0

end if

if the element at [row][column] has already been filled then

Set row and column to their previous values

Decrement row end if end for

Here is the 5 × 5 square that you get if you follow this algorithm:

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

Write a program whose input is the number n and whose output is the magic square of order n if n is odd.

Please solve in Python

Solutions

Expert Solution

PYTHON Program:

n= int(input("Enter the value of n: "))
if n%2==0:
print("Even value entered. Expecting an odd number.")
else:
arr= [[0 for i in range(n)] for j in range(n)]
row = n-1
column = n//2
for k in range(1,n*n+1):
arr[row][column] = k
newrow = row + 1
newcolumn = column + 1
if newrow == n:
newrow = 0
if newcolumn == n:
newcolumn = 0
if arr[newrow][newcolumn] != 0:
row = row-1
else:
row = newrow
column = newcolumn
  
print("Magic Square of order",n,"x",n,":")
for i in range(0,n):
for j in range(0,n):
print(arr[i][j],end=" ")
print(" ")

Output:


Related Solutions

THIS IS JAVA Magic squares. An n × n matrix that is filled with the numbers...
THIS IS JAVA Magic squares. An n × n matrix that is filled with the numbers 1, 2, 3, . . ., n^2 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. Write a program that randomly generates 16 numbers, and it assigns them to the array after testing that the number was not already assigned. The program should test whether they form a...
Implement the Banker's algorithm for deadlock avoidance, that works on a given set of N processes...
Implement the Banker's algorithm for deadlock avoidance, that works on a given set of N processes and M resource types (N<10,M<10). Use C/C++/C# or Java for the implementation, with a simple text interface, where the user enters only the name of the input file (text only). The program reads all the necessary input data from that file. The input data and result is then displayed on the screen. You may use your program to validate the example you gave in...
Consider the following recursive algorithm for computing the sum of the first n squares: S(n) =...
Consider the following recursive algorithm for computing the sum of the first n squares: S(n) = 12 +22 +32 +...+n2 . Algorithm S(n) //Input: A positive integer n //Output: The sum of the first n squares if n = 1 return 1 else return S(n − 1) + n* n a. Set up and solve a recurrence relation for the number of times the algorithm’s basic operation is executed. b. How does this algorithm compare with the straightforward non-recursive algorithm...
Implement the following pseudocode in Python Consider the following pseudocode for a sorting algorithm: STOOGESORT(A[0 ......
Implement the following pseudocode in Python Consider the following pseudocode for a sorting algorithm: STOOGESORT(A[0 ... n - 1]) if (n = 2) and A[0] > A[1] swap A[0] and A[1] else if n > 2 m = ceiling(2n/3) STOOGESORT(A[0 ... m - 1]) STOOGESORT(A[n - m ... n - 1]) STOOGESORT(A[0 ... m - 1])
In python Write a program to implement RSA algorithm based on the public key. def encryptmessage():...
In python Write a program to implement RSA algorithm based on the public key. def encryptmessage(): def decryptmessage(): encryptmessage () decryptmessage () No in-built functions and third-party APIs will be allowed.
Write, in Python, a recursive algorithm that takes, as input, a positive integer n, and returns,...
Write, in Python, a recursive algorithm that takes, as input, a positive integer n, and returns, as output, the sum of the first n positive odd integers. Your solution should be recursive, and it should not contain any "for" loops or "while" loops.
1. Implement the recursive LU factorization algorithm in Python. Use plenty of comments to explain your...
1. Implement the recursive LU factorization algorithm in Python. Use plenty of comments to explain your code. While you are coding, it is helpful to break up your code into sub-functions and test the sub-functions as you go along.
Implement the recursive LU factorization algorithm in Python. Use plenty of comments to explain your code....
Implement the recursive LU factorization algorithm in Python. Use plenty of comments to explain your code. While you are coding, it is helpful to break up your code into sub-functions and test the sub-functions as you go along.
Implement the CPU scheduling algorithm MLFQ in python. Have the program answer the table. Multilevel Feedback...
Implement the CPU scheduling algorithm MLFQ in python. Have the program answer the table. Multilevel Feedback Queue (absolute priority in higher queues)             Queue 1 uses RR scheduling with Tq = 5             Queue 2 uses RR scheduling with Tq = 10             Queue 3 uses FCFS All processes enter first queue 1. If time quantum (Tq) expires before CPU burst is complete, the process is downgraded to next lower priority queue. Processes are not downgraded when preempted by a...
Implement Radix Sort using PYTHON programming language. Use one of the two options for the algorithm...
Implement Radix Sort using PYTHON programming language. Use one of the two options for the algorithm to sort the digits: Use Counting Sort or Bucket Sort. • Assume the numbers are maximum 4-digit numbers. • If using Counting Sort, you can see that your digit range is between 0 and 9 ([0…9]). • If using Bucket Sort, you will have ten buckets labeled from 0 to 9. Please add comments and explain every step carefully.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT