# Weird Numbers-Square-Thingy

Discussion in 'General Programming Support' started by Accname, May 25, 2016.

1. ### Accname2D-Graphics enthusiast

Ratings:
+1,551 / 4 / -4
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: May 25, 2016
2. ### tom_mai78101The Helper Connoisseur / Ex-MineCraft HostStaff Member

Ratings:
+984 / 4 / -1
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.

3. ### Accname2D-Graphics enthusiast

Ratings:
+1,551 / 4 / -4
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.

4. ### tom_mai78101The Helper Connoisseur / Ex-MineCraft HostStaff Member

Ratings:
+984 / 4 / -1
Oh, my mistake. I also didn't notice the numbers are increasing while zigzagging across the grid.

5. ### jonasWell-Known Member

Ratings:
+42 / 4 / -0
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.