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>

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.