Weird Numbers-Square-Thingy

Accname

2D-Graphics enthusiast
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
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
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
Oh, my mistake. I also didn't notice the numbers are increasing while zigzagging across the grid.

jonas

Ultra Cool Member
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.
• Jesus4Lyf:
Hmm, how to summarize... Moved to the USA and got married. Became a professional software engineer and solution lead. Learning Godot for game dev.
• jonas:
So you are pursuing game dev as a hobby? What industry is your dayjob in?
• jonas:
FAANG?
• Jesus4Lyf:
Not quite FAANG, less pay for a less hours/stress. Yeah, game dev as a hobby.
• jonas:
makes sense. Do we have a hobby game dev subforum to showcase some work?
• Blackveiled:
Happy Thursday
+1
• The Helper:
Happy Thursday!
• The Helper:
TGIF!
• The Helper:
hey tom_mai please do not archive anymore news
• Ghan:
AMD Ryzen 7000 next week
+1
• The Helper:
noice!
• tom_mai78101:
"hey tom_mai please do not archive anymore news" ----> Got it. What should we do with the "News Archive" subforum?
• The Helper:
I think leave it for real old news that cannot be sorted the reason we had it in the first place was a server issue that we do not have anymore. I don't want to get rid of the archive just not put anything in it for now until we figure it out more
• The Helper:
The side chat has nothing on the discord. it is not possible but the biggest thing on my wish list right now is a sidechat channel on the disord that hooks up to this...
• Ghan:
I've been selected for the Diablo IV beta.
+1
• Ghan:
Woo!
+1
• The Helper:
that is so cool
• V-SNES:
Eh just noticed the new discord section on the side. Looks great!
+1
• Blackveiled:
feels bad I can't ever get selected for any beta I sign up for. It's annoying as heck.
+1
• The Helper:
Hello mgarcia! Happy Monday everyone!
• The Helper:
Happy Tuesday!

Members online

No members online now.