Skip to content

Instantly share code, notes, and snippets.

@Kornel
Last active December 3, 2017 09:21
Show Gist options
  • Save Kornel/b4b8098c7d23100d1a74ec0ae5b81074 to your computer and use it in GitHub Desktop.
Save Kornel/b4b8098c7d23100d1a74ec0ae5b81074 to your computer and use it in GitHub Desktop.
Ulam's spiral (sort of, without primes yet)
import static java.lang.Math.sqrt;
public class S {
public static void main(String[] args) {
int[][] s = createSpiral(20);
print(s);
}
private static int[][] createSpiral(int n) {
int[][] t = new int[n][n];
int shiftx = n / 2;
int shifty = shiftx - 1;
int x = 0;
int y = 0;
for (int i = 1; i <= n * n; ++i) {
t[x + shiftx][y + shifty] = i;
y = y + dy(turn(i + 1));
x = x + dx(turn(i + 1));
}
return t;
}
private static void print(int[][] s) {
for (int i = 0; i < s.length; ++i) {
for (int j = 0; j < s.length; ++j) {
System.out.print(s[i][j]);
System.out.print("\t");
}
System.out.println("");
}
}
private static int turn(int n) {
return (int)sqrt(4 * n - 7) % 4;
}
private static int dx(int turn) {
if (turn == 0) return 1;
if (turn == 1) return 0;
if (turn == 2) return -1;
if (turn == 3) return 0;
throw new IllegalStateException("Unexpected x turn " + turn);
}
private static int dy(int turn) {
if (turn == 0) return 0;
if (turn == 1) return 1;
if (turn == 2) return 0;
if (turn == 3) return -1;
throw new IllegalStateException("Unexpected y turn " + turn);
}
}
@Kornel
Copy link
Author

Kornel commented Nov 30, 2016

400	399	398	397	396	395	394	393	392	391	390	389	388	387	386	385	384	383	382	381
325	324	323	322	321	320	319	318	317	316	315	314	313	312	311	310	309	308	307	380
326	257	256	255	254	253	252	251	250	249	248	247	246	245	244	243	242	241	306	379
327	258	197	196	195	194	193	192	191	190	189	188	187	186	185	184	183	240	305	378
328	259	198	145	144	143	142	141	140	139	138	137	136	135	134	133	182	239	304	377
329	260	199	146	101	100	99	98	97	96	95	94	93	92	91	132	181	238	303	376
330	261	200	147	102	65	64	63	62	61	60	59	58	57	90	131	180	237	302	375
331	262	201	148	103	66	37	36	35	34	33	32	31	56	89	130	179	236	301	374
332	263	202	149	104	67	38	17	16	15	14	13	30	55	88	129	178	235	300	373
333	264	203	150	105	68	39	18	5	4	3	12	29	54	87	128	177	234	299	372
334	265	204	151	106	69	40	19	6	1	2	11	28	53	86	127	176	233	298	371
335	266	205	152	107	70	41	20	7	8	9	10	27	52	85	126	175	232	297	370
336	267	206	153	108	71	42	21	22	23	24	25	26	51	84	125	174	231	296	369
337	268	207	154	109	72	43	44	45	46	47	48	49	50	83	124	173	230	295	368
338	269	208	155	110	73	74	75	76	77	78	79	80	81	82	123	172	229	294	367
339	270	209	156	111	112	113	114	115	116	117	118	119	120	121	122	171	228	293	366
340	271	210	157	158	159	160	161	162	163	164	165	166	167	168	169	170	227	292	365
341	272	211	212	213	214	215	216	217	218	219	220	221	222	223	224	225	226	291	364
342	273	274	275	276	277	278	279	280	281	282	283	284	285	286	287	288	289	290	363
343	344	345	346	347	348	349	350	351	352	353	354	355	356	357	358	359	360	361	362

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment