-
Notifications
You must be signed in to change notification settings - Fork 0
/
4. The Ulam Spiral.java
80 lines (72 loc) · 2.28 KB
/
4. The Ulam Spiral.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
import java.util.Arrays;
import java.util.Scanner;
class Main
{
enum Direction{
RIGHT, UP, LEFT, DOWN;
}
private static String[][] genUlam(int n){
int count = 1, count2 = 1;
while(count2 < n){
count+=2;
count2 = count * count;
}
return genUlam(count, 1, n);
}
private static String[][] genUlam(int n, int i, int input2){
String[][] spiral = new String[n][n];
Direction dir = Direction.RIGHT;
int j = i;
int y = n / 2;
int x = (n % 2 == 0) ? y - 1 : y;
while(j <= ((n * n) - 1 + i)){
if(j <= input2){
spiral[y][x] = String.valueOf(j);
spiral[y][x] = isPrime(j) ? "#" : ".";
}else{
spiral[y][x] = "";
}
switch(dir){
case RIGHT: if(x <= (n - 1) && spiral[y - 1][x] == null && j > i) dir = Direction.UP; break;
case UP: if(spiral[y][x - 1] == null) dir = Direction.LEFT; break;
case LEFT: if(x == 0 || spiral[y + 1][x] == null) dir = Direction.DOWN; break;
case DOWN: if(spiral[y][x + 1] == null) dir = Direction.RIGHT; break;
}
switch(dir){
case RIGHT: x++; break;
case UP: y--; break;
case LEFT: x--; break;
case DOWN: y++; break;
}
j++;
}
return spiral;
}
public static boolean isPrime(int a){
if(a == 2) return true;
if(a <= 1 || a % 2 == 0) return false;
long max = (long)Math.sqrt(a);
for(long n = 3; n <= max; n += 2){
if(a % n == 0) return false;
}
return true;
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int input = sc.nextInt();
if(input != 1){
String[][] ulam = genUlam(input);
for(String[] row : ulam){
if(row != null){
String out = Arrays.toString(row).replaceAll(",", "");
out = out.replace("[", "").replace("]", "").replace(" ", "");
if (out.length()>0){
System.out.println(out);
}
}
}
}else{
System.out.println(".");
}
}
}