Full answer pls 2.A magic square is a square array of numbers where the sum of t
ID: 664797 • Letter: F
Question
Full answer pls
2.A magic square is a square array of numbers where the sum of the numbers across each row down each column, or along either of the main diagonals is the same. For example: 11 24 7 20 3 4 12 258 16 17513 21 9 10181 14|22 23 619 2 15 Here is an algorithm for creating a (2n-1) × (2n-1) magic square: we begin by entering 1 at the top-left corner and moving diagonally down and to the right by one square at each step that we can, wrapping around the edges of the board as needed. When we reach a filled-in square, we move down by two squares and then return to the diagonal moves. At the end of the process, we reset the entire square so that the middle square holds the average value. The two squares above were created using this algorithm. This is what they looked like before we shifted the 5 and 13 to the centres: 114 22 10 18 19215 23 6 7 203 11 24 25 816 4 12 13 219175 9 2 4 5 73 (a) Show that the algorithm fills every square in the array with the numbers from 1 to (2n -1)2 before arriving again at the beginning square. (b) Show that every row and every column have exactly one element of each equivalence class of the integers modulo (2n -1). (c) In class, we created an equivalence relation with classes (1,2,3, , 2n -1), [2n, 2n+ 1,2n +2,... , 4n -2), etc. Show that every row and every column have exactly one element of each equivalence class (d) Show that (a) - (c) imply that every row and every column has the same sum (you may find the arithmetic neater if you use 2n - 1 as the representative of the equivalence class 0) (e) Part (d) tells us that resetting the large square was only done to get the diagonals to have the correct sum. Show that the shift does complete the magic square. Hint: One diagonal is only rearranged, while the other one becomes quite easy. Some thought will yield easy proofs of this. () When we take the values in the magic square modulo (2n -1) and use (2n - 1) for the class 0, we can interpret each value as a column number in the (2n-1) × (2n-1) chessboard. Therefore, we can take an entire row or column as describing a set of queens, with the t number being the column number for the queen on row i. Prove that the result along any row or column of the magic square as filled in by our algorithm gives a solution to the queen independence problem.Explanation / Answer
I have created a very simple c code for this problem. This code will answer all the questions. you can try the code for all odd numbers and hence you can prove everything by induction.
#include<stdio.h>
#include<string.h>
void generateSquare(int n)
{
int magicSquare[n][n];
// set all slots as 0
memset(magicSquare, 0, sizeof(magicSquare));
// Initialize position for 1
int i = n/2;
int j = n-1;
// One by one put all values in magic square
for (int num=1; num <= n*n; )
{
if (i==-1 && j==n) //3rd condition
{
j = n-2;
i = 0;
}
else
{
//1st condition helper if next number goes to out of square's right side
if (j == n)
j = 0;
//1st condition helper if next number is goes to out of square's upper side
if (i < 0)
i=n-1;
}
if (magicSquare[i][j]) //2nd condition
{
j -= 2;
i++;
continue;
}
else
magicSquare[i][j] = num++; //set number
j++; i--; //1st condition
}
// print magic square
printf("The Magic Square for n=%d: Sum of each row or column %d: ",
n, n*(n*n+1)/2);
for(i=0; i<n; i++)
{
for(j=0; j<n; j++)
printf("%3d ", magicSquare[i][j]);
printf(" ");
}
}
int main()
{
int n = 5;
generateSquare (n);
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.