I have a code on the Internet and I have rewritten it, but there are t and n3 variable in the odd_order function that I don't understand their meaning. Please help me to explore it. :D. This code is generating a odd order magic square.

<html>
    <head>
        <title>Magic Squares</title>
        <script language=JavaScript>
            <!-- hide
            var matrix;
            // compute ...
            function compute() {
                var i;
                var N = parseInt(calc.square.value, 10);
                if (isNaN(N)||(N < 1)) {
                    window.alert("Invalid input!");
                    return;
                } // end if
                matrix = new Array(N);
                for (i = 0; i < N; i++) {
                    matrix[i] = new Array(N);
                    for (j = 0; j < N; j++)
                    matrix[i][j] = parseInt("0");
                } // end for
                if ((N %2) == 0) {
                    window.alert("This program cannot calculate the even magic square!");
                    return;                     
                }
                open_window("magic square"); // open one another window
                msgWindow.document.writeln(N + " x " + N + " square:"); // title for content (Ex: 3x3 square)
                msgWindow.document.writeln(""); // write a break
                make_magic(N); // call make_magic function with passing argument N into it
            } // end compute()
            //.............................................................................................................

            // create a magic square
            function make_magic(n) { // parameter is the argument passed from computer()
                odd_order(n);
                put_square(n);
                msgWindow.document.writeln("");

    //if (is_magic(n))
      //  msgWindow.document.writeln("Is magic; the sum of each row, column, and main diagonal is " + Math.floor((n*(n*n+1))/2));
    //else
      //  msgWindow.document.writeln("Is not a magic square!");
            } // end make_magic()
            //.............................................................................................................

            // siamese method for any odd value n
            function odd_order(n) { // parameter is the argument passed from make_magic() 
                var i, j, n3, t;
                n3 = Math.floor((n - 3)/2);
                for (i = 1; i <= n; i++) {
                    for (j = 1; j <= n; j++) {
                        t = (i + j*2 - 2)%n;
                        matrix[i-1][j-1] = 1 + t + n*((n3 + i + j)%n);
                    } // end inner for
                } // end outter for
            } // end odd_order
            //.............................................................................................................
            // sends square to text output
            function put_square( n ) {
                var i, j;
                var w = n*n;
                var x = w.toString(10);
                var width = x.length + 2;
                for (i = 0; i < n; i++) {
                    for (j = 0; j < n; j++)
                        msgWindow.document.write(cintstr(matrix[i][j], width));
                    msgWindow.document.writeln("");
                } // end for      
            }
         //.............................................................................................................
            // format an integer
            function cintstr( num, width ) {
                var str = num.toString(10);
                var len = str.length;
                var intgr = "";
                var i;
                // append leading spaces
                for (i = 0; i < width - len; i++)
                    intgr += ' ';
                // append digits
                for (i = 0; i < len; i++)
                    intgr += str.charAt(i);
                return intgr;
            } // end cintstr
        //.............................................................................................................
        // opens a separate browser window
        function open_window( name ) {
            msgWindow = window.open("","msgWindow","toolbar=no,status=no,menubar=yes,scrollbars=yes,width=550,height=400,resizable=yes");
            msgWindow.document.open();
            msgWindow.document.writeln("<html><head><title>" + name + "</title></head>");
            msgWindow.document.writeln("<body>");
            msgWindow.document.writeln("<pre>");
        } // end open_window
        </script>
    </head>

    <body bgcolor="white" text="black">
        <center>
            <form name=calc>
                <table border=1 cellspacing=0 bordercolor=darkblue frame=border rules=rows>
                    <tr><td align=right>Size: </td><td>
                    <INPUT size=10 maxLength=20 name=square>
                </table>
            </form>
            <INPUT onclick=compute(); type=button value=Compute>
        </center>
    </body>
</html>

Dani AI

Generated

In that compact arithmetic version of the Siamese method posted by , the two variables have simple, distinct jobs. n3 is a fixed shift derived from the order n (it repositions which “band” or block of n a cell belongs to). t is the small intra-block offset that cycles through 0..n-1 based on the row and column. The code builds each cell by combining an intra-block residue (t) with n times a block index, plus 1, so every integer from 1..n^2 appears exactly once.

Step-by-step view: the routine computes two cyclic indices for each 1-based (i,j) position. One index (t) picks the position inside the current n-sized cycle. The other index is a band/block selector formed from n3 plus i and j, reduced modulo n. Multiplying that block index by n and adding the intra-block offset (and 1) gives the final entry. The modulus wraps both cycles so the pattern repeats correctly; choosing n3 = floor((n-3)/2) aligns the bands so the Siamese pattern produces the magic-sum property (each row, column and main diagonal sums to the same constant).

Practical tips: the loops are written 1..n but JavaScript arrays are 0-based, so watch the matrix[i-1][j-1] indexing to avoid off-by-one errors. Always use parseInt(..., 10) when parsing input. If you ever see negative remainders use ((x % n) + n) % n to normalize them. For debugging test small odd n (3 and 5) and verify the magic constant n(nn+1)/2. Rename n3->shift and t->offset to improve readability. correctly moved this to JavaScript; is right that the code is compact — it’s a concise arithmetic encoding of the standard Siamese construction.

Recommended Answers

All 2 Replies

I think this is html and javascript problem, not scientific question, so I move it to JavaScript section.

This code is compressed. You have to either ask the originator or try to un compress it.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.