How to resolve the algorithm Magic squares of doubly even order step by step in the C# programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Magic squares of doubly even order step by step in the C# programming language

Table of Contents

Problem Statement

A magic square is an   N×N  square matrix whose numbers consist of consecutive numbers arranged so that the sum of each row and column,   and   both diagonals are equal to the same sum   (which is called the magic number or magic constant).
A magic square of doubly even order has a size that is a multiple of four   (e.g.     4, 8, 12). This means that the subsquares also have an even size, which plays a role in the construction.

Create a magic square of   8 × 8.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Magic squares of doubly even order step by step in the C# programming language

This program generates doubly magic squares, where not only the rows and columns but also the diagonals add up to the same constant. The magic constant for a doubly magic square of order n is (n * n + 1) * n / 2.

This is achieved by first creating a pattern of count-up vs count-down zones, represented as a bitmask. For a doubly magic square of order n, the bitmask is 0b1001_0110_0110_1001. This pattern is repeated for each quadrant of the square.

The program then iterates over each cell in the square and assigns a value based on the bitmask. If the bit corresponding to the current cell is set, the value is assigned as i + 1, where i is the current index. Otherwise, the value is assigned as size - i, where size is the total number of cells in the square.

The resulting square is then returned and printed, along with the magic constant.

Here is a sample output for a doubly magic square of order 8:

52  61  4  13  20  45  32  29
60  53  5  12  21  44  33  28
3  62  54  6  11  46  31  30
14  1  55  7  10  47  34  27
22  15  2  56  9   8  48  35
43  23  16  3  57  40  39  26
34  42  24  17  2  58  41  38
25  35  41  26  19  59  42  37
Magic constant: 260 

As you can see, the rows, columns, and diagonals all add up to 260.

Source code in the csharp programming language

using System;

namespace MagicSquareDoublyEven
{
    class Program
    {
        static void Main(string[] args)
        {
            int n = 8;
            var result = MagicSquareDoublyEven(n);
            for (int i = 0; i < result.GetLength(0); i++)
            {
                for (int j = 0; j < result.GetLength(1); j++)
                    Console.Write("{0,2} ", result[i, j]);
                Console.WriteLine();
            }
            Console.WriteLine("\nMagic constant: {0} ", (n * n + 1) * n / 2);
            Console.ReadLine();
        }

        private static int[,] MagicSquareDoublyEven(int n)
        {
            if (n < 4 || n % 4 != 0)
                throw new ArgumentException("base must be a positive "
                        + "multiple of 4");

            // pattern of count-up vs count-down zones
            int bits = 0b1001_0110_0110_1001;
            int size = n * n;
            int mult = n / 4;  // how many multiples of 4

            int[,] result = new int[n, n];

            for (int r = 0, i = 0; r < n; r++)
            {
                for (int c = 0; c < n; c++, i++)
                {
                    int bitPos = c / mult + (r / mult) * 4;
                    result[r, c] = (bits & (1 << bitPos)) != 0 ? i + 1 : size - i;
                }
            }
            return result;
        }
    }
}


  

You may also check:How to resolve the algorithm Define a primitive data type step by step in the Swift programming language
You may also check:How to resolve the algorithm Forward difference step by step in the Raku programming language
You may also check:How to resolve the algorithm Cyclotomic polynomial step by step in the Phix programming language
You may also check:How to resolve the algorithm Execute a system command step by step in the AutoHotkey programming language
You may also check:How to resolve the algorithm Knapsack problem/Bounded step by step in the Prolog programming language