Weird Numbers-Square-Thingy

Accname

2D-Graphics enthusiast
Reaction score
1,462
Here is the code for TWV's weird number square thingy: (Java)
Code:
public class WeirdNumberSquareThingy {
 
   public static void main(String[] args) {
     System.out.println(new WeirdNumberSquareThingy(5));
     /* Prints:
     [  1 |  2 |  6 |  7 | 15]
     [  3 |  5 |  8 | 14 | 16]
     [  4 |  9 | 13 | 17 | 22]
     [ 10 | 12 | 18 | 21 | 23]
     [ 11 | 19 | 20 | 24 | 25]
      */
   }
 
   private final int[] table;
   private final int size;
 
   public WeirdNumberSquareThingy(int size) {
     this.size = size;
     table = new int[size * size];
   
     int num = 1;
     int steps = 1;
     int x = 0;
     int y = 0;
     boolean growing = true;
   
     while (num <= table.length) {
       for (int i = 1; i < steps; i++) {
         set(x, y, num++);
         if (steps % 2 == 1) {
           x += 1;
           y -= 1;
         } else {
           x -= 1;
           y += 1;
         }
       }
       if (steps == size) {
         growing = false;
       }
       set(x, y, num++);
       if (growing == (steps % 2 == 1)) {
         x += 1;
       } else {
         y += 1;
       }
     
       if (growing) {
         steps++;
       } else {
         steps--;
       }
     }
   }
 
   private void set(int x, int y, int number) {
     table[x + y * size] = number;
   }
 
   public int get(int x, int y) {
     return table[x + y * size];
   }
 
   public String toString() {
     StringBuilder sb = new StringBuilder();
   
     for (int y = 0; y < size; y++) {
       sb.append("[ ");
       for (int x = 0; x < size; x++) {
         int num = get(x, y);
         if (num < 10) {
           sb.append(" ");
         }
         sb.append(num);
         if (x != size -1) {
           sb.append(" | ");
         }
       }
       sb.append("]\n");
     }
   
     return sb.toString();
   }
 
}
 
Last edited:

tom_mai78101

The Helper Connoisseur / Ex-MineCraft Host
Staff member
Reaction score
1,677
I feel @thewrongvine wanted the exact order from bottom left, counting to the top right, row major.

And where exactly is the origin located for the main platform? Usually, it's top left for origin.
 

Accname

2D-Graphics enthusiast
Reaction score
1,462
There is no "top-left" or "bottom-left" or any other corner within the code. arrays do not have a left or right, a top or bottom. Its all what you make of it.
Now the toString() method I wrote may put the top-left as the origin. But you can just as easily use any other point as the origin by simply printing it out in a different order.
 

tom_mai78101

The Helper Connoisseur / Ex-MineCraft Host
Staff member
Reaction score
1,677
Oh, my mistake. I also didn't notice the numbers are increasing while zigzagging across the grid.
 

jonas

You can change this now in User CP.
Reaction score
67
http://www.HostMath.com/Show.aspx?C...w(x,y) & \text{o.w.} \end{cases} \end{align*}



x,y start from 0 and go up to size-1.

Code:
Lemma 1: row(r) has the number of fields before row r.
Proof by induction on r. 
  r = 0: row(0) = lowrow(0) = 0
  r -> r+1: if r<=size: row r adds r new fields, gaussian sum works
if r = size+1: both branches of the definition agree for r, proof below works
if r > size+1: row r adds 2*size-r new fields, gaussian sum gives that this is exactly the difference


Theorem: val(x,y) gives the right value
by induction on x.
 x = 0, x+y odd: starts with total number of fields until (including) row x+y
 x = 0, x+y even: first field of row x+y
 x -> x+1: old diagonal: we decrease y by one: decreases by one in odd fields, increases by one in even fieds, x+y stays stable, fine.
                   new diagonal: then x+y >= size, and y = size -1, so you get the first field, proof like x=0.
 
General chit-chat
Help Users
  • No one is chatting at the moment.

      The Helper Discord

      Members online

      No members online now.

      Affiliates

      Hive Workshop NUON Dome World Editor Tutorials

      Network Sponsors

      Apex Steel Pipe - Buys and sells Steel Pipe.
      Top