JAVASCRIPT METHODS

GENERATING THE RIGHT DIAGONALS FOR MAGIC SQUARE OF SQUARES (Part VA)

Picture of a square

Introduction

This site shows how the tables in Part IA through Part IVE for generating squares of squares were produced. I will show only the equations and the conditional statements used for calculating the a2, b2 and c2as well as f from the middle group of tables. e and g are also shown where e is the difference between adjacent b1s and g is the difference between adjacent c1s. In addition, e is found by empirical means, viz., by plugging in the minimum value in equation f below. The value of g follows from the equation g = 2 × e. Output to the screen are typical document.write statements.

Four methods are introduced where:

b1 =  k
b1 = −k
c1 =  k
c1 = −k

where k is any natural integer from 1 to ∞ and m is an integer derived from k which is used to calculate d = 2(2b1c1 − 1 ), the denominator in the equation:

f = [2e2n2 + (4c1 − 4b1) en +(1 − 2b12 + c12)] / {2(2b1 − c1 − 1)}

m is also used in only two of the methods to generate nL which is derived from log (m).

Two functions in particular are used in the conditional statements to test whether a number is an integer or is odd-even, viz., isInteger(value) and isEven(value) shown below:


function isInteger(s)
{
   return (s.toString().search(/^-?[0-9]+$/) == 0);
}


function isEven(value)
{
  if (value%2 == 0) return true;
  else return false;
}


Method I where b1 = k

In Method I where initially a1 = 1, b1 = k and c1 = 1 the following equations apply:

m = k − 1
d = 4*m
f = [a1*a1 + b1*b1 + c1*c1 − 3*b1*b1] /d
a = a1 + f
b = b1 + f
c = c1 + f
Δ = b1*b1a1*a1

Method I does not use either the isInteger() or isEven() function but uses instead a switch statement to differentiate between the different possibilities for e and, therefore, g. It first sets the variable j to

j = k mod 64 (the remainder left after dividing by 64)
and then uses j as a parameter in the multi-conditional statement switch(), the default being those numbers which are mod to some other number,
e.g., 5 which leaves a remainder of 5.

     switch(j)
       {
         case  1: e=d/16;  g=2*e; break;
         case  9: e=d/8;   g=2*e; break;
         case 17: e=d/8;   g=2*e; break;
         case 25: e=d/8;   g=2*e; break;
         case 33: e=d/16;  g=2*e; break;
         case 41: e=d/8;   g=2*e; break;
         case 49: e=d/8;   g=2*e; break;
         case 57: e=d/8;   g=2*e; break;
         default: e=d/4;   g=2*e; break;
        }

which means that certain numbers will have e = d/16, others to e = d/8 and the rest e = d/4.

To generate the requisite output to the screen since we know e and g we can use a for statement to generate the required tables in all four methods. The for statement includes

b1 = b1 + e
c1 = c1 + g
where again the new b1 and c1 values correspond to the next line in the table after adding e and g, respectively.
In addition, all the output statements, except for the first two, listed above are also included:
m = ...
...
Δ = ...

Method II where b1 = −k

In Method II where initially a1 = 1, b1 = −k and c1 = 1 the following equations apply:

m = k + 1
d = 4*m
nL = [Math.log(m)/Math.log(2)] + 1
f = [a1*a1 + b1*b1 + c1*c1 − 3*b1*b1] /d
a = a1 + f
b = b1 + f
c = c1 + f
Δ = b1*b1a1*a1

Method II uses both the isInteger() or isEven() function in the conditional statements to generate both e and g.
It uses a log(m)/log(2) statement which when added to 1 produces an integer which may be either even or odd to produce the requisite e and g values. Those where nL ≠ an integer will produce a different value for e and g. Conditional if-then-else statements are used to determine the values of e and g as follows:


 if (isInteger(nL))
       {
         x=(nL + nL%2)/2;
         y=Math.pow(2,x);
         if (isEven (nL))  /* n may be even or odd after doing the log calc. */
             {e=-d/(2*y); g=2*e;}  /* if nL is even */
         else
             {e=-d/y; g=2*e;}      /* if nL is odd */
       }

  else {e= -d/4; g=2*e;}         /* if nL is not an integer */
  1. If (nL is both an integer and even) then x is set to (nL + nL mod 2) and 2x is used to derive y. e is divided by 2y
  2. If (nL is both an integer and odd) then e is divided by y
  3. If nL is not an integer then d is divided by 4 to obtain e

To output the table see the above section.


Method III where c1 = k

In Method III where initially a1 = 1, b1 = 1 and c1 = k the following equations apply:

m = (k − 1)/2
d = −4*m
f = [a1*a1 + b1*b1 + c1*c1 − 3*b1*b1] /d
a = a1 + f
b = b1 + f
c = c1 + f
Δ = b1*b1a1*a1

Method III uses both the isInteger() or isEven() function in the conditional statements to generate both e and g.
It uses conditional if-then-else statements to determine the values of e and g as follows:


     n1 = (k-1)/4;
     n2 = (k-3)/4;

     if (isEven(k)) {e= -d; g=2*e;}

       else if (k%16==1 && isInteger(n1)) {e=-d/8;g=2*e;}
     
         else if (isInteger(n2) && isInteger(Math.sqrt(-d))) {e=Math.sqrt(-d); g=2*e;}
            
             else if (isInteger(n1)) {e=-d/4; g=2*e;}

                  else if (isInteger(n2)) {e=-d/2; g=2*e;} 

These statements appear overwhelming but are easily explained.

  1. n1 and n2 are set equal to (k − 1)/4 and (k − 3)/4, respectively and only one of these numbers will be an integer while the other is a fraction.
  2. In the first condition if k is even then e = −d

    In the next four else statements k is odd and either n1 or n2 are part of the conditions.
  3. In (c) and (d) n1 and n2 along with some other condition are filtered out before applying (e) and (f) where n1 and n2 generate a different e. These values were determined empirically by application of the f equation above.

  4. If n1 is an integer and (k mod 16) = 1 then e = −d/8
  5. If n2 is an integer and the √−d is an integer then e = √−d
  6. If n1 is an integer then e = −d/4
  7. If n2 is an integer then e = −d/2

To output the table see the above section.


Method IV where c1 = −k

In Method IV where initially a1 = 1, b1 = 1 and c1 = −k the following equations apply:

m = (k + 1)/2
d = 4*m
nL = [Math.log(m)/Math.log(2)] + 1
f = [a1*a1 + b1*b1 + c1*c1 − 3*b1*b1] /d
a = a1 + f
b = b1 + f
c = c1 + f
Δ = b1*b1a1*a1

Method IV uses both the isInteger() or isEven() function in the conditional statements to generate both e and g.
It uses conditional if-then-else statements to determine the values of e and g as follows:


     n1 = (k-1)/4;
     n2 = (k-3)/4;

     if (isInteger(nL))
       {
         x=(nL-nL%2)/2;
         y=Math.pow(2,x);
         e=(k+1)/y; g=2*e;
       }

      else if (isEven(k))  {e=d/2; g=2*e;}
         else if (isInteger(Math.sqrt(d))) {e=Math.sqrt(d);g=2*e;}
            else if (k%16==15 && isInteger(n2)) {e=d/8;g=2*e;}
               else if (isInteger(n1)) {e=d/2;g=2*e;}
                  else if (isInteger(n2)) {e=d/4;g=2*e;}  

These statements again appear overwhelming but are easily explained.

  1. n1 and n2 are set equal to (k − 1)/4 and (k − 3)/4, respectively and only one of these numbers will be an integer while the other is a fraction
  2. In the first condition if nL is an integer then x is set to (nLnL mod 2) and 2x is used to derive y. e is then equal to (k+1)/y
  3. In the second condition if k is even then e = d/2
  4. In the third condition k is odd and the √d is an integer.
  5. In the next three else statements k is odd and either n1 or n2 are part of the conditions.
    In (e) n2 along with some other condition is filtered out before applying (f) and (g) where n1 and n2 generate a different e. These values were determined empirically by application of the f equation above.

  6. If n2 is an integer and (k mod 16) = 15 then e = d/8
  7. If n1 is an integer then e = d/2
  8. If n2 is an integer then e = d/4

To output the table see the above section.

This concludes Part VA. To go back to Part IVC.
Go back to homepage.


Copyright © 2012 by Eddie N Gutierrez. E-Mail: Fiboguti89@Yahoo.com