lib/numbers/matrix.js
function (M) { //starting at bottom right, moving horizontally
    var jump = false,
      tl = n * n,
      br = 1,
      inc = 1,
      row, col, val, i, j;
    M[0][0] = tl;
    M[n - 1][n - 1] = br;

    for (i = 1; i < n; i++) {
      //generate top/bottom row
      if (jump) {
        tl -= 4 * inc;
        br += 4 * inc;
        inc++;
      } else {
        tl--;
        br++;
      }

      M[0][i] = tl;
      M[n - 1][n - 1 - i] = br;
      jump = !jump;
    }

    var dec = true;
    for (i = 1; i < n; i++) {
      //iterate diagonally from top row
      row = 0;
      col = i;
      val = M[row][col];

      for (j = 1; j < i + 1; j++) {
        if (dec) {
          val -= 1;
        } else {
          val += 1;
        }
        row++;
        col--;
        M[row][col] = val;
      }
      dec = !dec;
    }

    if (n % 2 === 0) {
      dec = true;
    } else {
      dec = false;
    }
    for (i = 1; i < n - 1; i++) {
      //iterate diagonally from bottom row
      row = n - 1;
      col = i;
      val = M[row][col];

      for (j = 1; j < n - i; j++) {
        if (dec) {
          val--;
        } else {
          val++;
        }
        row--;
        col++;
        M[row][col] = val;
      }
      dec = !dec;
    }
    return M;
  }
function (M) { //starting at bottom right, moving horizontally
    var jump = false,
      tl = n * n,
      br = 1,
      inc = 1,
      row, col, val, i, j;
    M[0][0] = tl;
    M[n - 1][n - 1] = br;

    for (i = 1; i < n; i++) {
      //generate top/bottom row
      if (jump) {
        tl -= 4 * inc;
        br += 4 * inc;
        inc++;
      } else {
        tl--;
        br++;
      }

      M[0][i] = tl;
      M[n - 1][n - 1 - i] = br;
      jump = !jump;
    }

    var dec = true;
    for (i = 1; i < n; i++) {
      //iterate diagonally from top row
      row = 0;
      col = i;
      val = M[row][col];

      for (j = 1; j < i + 1; j++) {
        if (dec) {
          val -= 1;
        } else {
          val += 1;
        }
        row++;
        col--;
        M[row][col] = val;
      }
      dec = !dec;
    }

    if (n % 2 === 0) {
      dec = true;
    } else {
      dec = false;
    }
    for (i = 1; i < n - 1; i++) {
      //iterate diagonally from bottom row
      row = n - 1;
      col = i;
      val = M[row][col];

      for (j = 1; j < n - i; j++) {
        if (dec) {
          val--;
        } else {
          val++;
        }
        row--;
        col++;
        M[row][col] = val;
      }
      dec = !dec;
    }
    return M;
  }
function ( M ) { //starting at bottom right, moving horizontally var jump = false , tl = n * n , br = 1 , inc = 1 , row , col , val , i , j ; M [ 0 ] [ 0 ] = tl ; M [ n - 1 ] [ n - 1 ] = br ; for ( i = 1 ; i < n ; i ++ ) { //generate top/bottom row if ( jump ) { tl -= 4 * inc ; br += 4 * inc ; inc ++ ; } else { tl -- ; br ++ ; } M [ 0 ] [ i ] = tl ; M [ n - 1 ] [ n - 1 - i ] = br ; jump = ! jump ; } var dec = true ; for ( i = 1 ; i < n ; i ++ ) { //iterate diagonally from top row row = 0 ; col = i ; val = M [ row ] [ col ] ; for ( j = 1 ; j < i + 1 ; j ++ ) { if ( dec ) { val -= 1 ; } else { val += 1 ; } row ++ ; col -- ; M [ row ] [ col ] = val ; } dec = ! dec ; } if ( n % 2 === 0 ) { dec = true ; } else { dec = false ; } for ( i = 1 ; i < n - 1 ; i ++ ) { //iterate diagonally from bottom row row = n - 1 ; col = i ; val = M [ row ] [ col ] ; for ( j = 1 ; j < n - i ; j ++ ) { if ( dec ) { val -- ; } else { val ++ ; } row -- ; col ++ ; M [ row ] [ col ] = val ; } dec = ! dec ; } return M ; }
create one kind of permutation - all other permutations can be created from this particular permutation through transformations
create one kind of permutation - all other permutations can be created from this particular permutation through transformations