-
Notifications
You must be signed in to change notification settings - Fork 0
/
primeSpiral.pde
114 lines (104 loc) · 2.14 KB
/
primeSpiral.pde
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
int stepsToTurn = 1;
int sameSteps = 0;
int sinceTurn = 0;
int px;
int py;
int lpx;
int lpy;
int step = 10; // change density
int dx = step;
int dy = 0;
int end;
void setup() {
size(500, 500); // change output size
background(100,150,255);
px = width/2;
py = height/2;
lpx = px;
lpy = py;
end = ((width/step)-1)*((height/step)-1);
for (int i = 1; i <= end; i++) {
strokeWeight(step/10);
stroke(255,255,255,75);
line(px, py, lpx, lpy);
if (isPrime(i-1) && i > 1) {
stroke(255);
strokeCap(ROUND);
strokeWeight(step/2);
point(lpx,lpy);
}
lpx = px;
lpy = py;
px += dx;
py += dy;
sinceTurn++;
if (stepsToTurn == sinceTurn) {
turn();
sinceTurn = 0;
sameSteps++;
}
if (sameSteps == 2) {
sameSteps = 0;
stepsToTurn++;
}
}
// uncomment if you want to save the result
// save("primeSpiral.png");
}
boolean isPrime(float num) {
boolean prime = true;
for (int i = 2; i <= sqrt(num); i++) {
if (num % i == 0) prime = false;
}
return prime && num > 1;
}
boolean notPrime(float num) {
boolean prime = true;
for (int i = 2; i <= sqrt(num); i++) {
if (num % i == 0) prime = false;
}
return !(prime && num > 1);
}
boolean perfectSqr(int num) {
return sq(floor(sqrt(num))) == num;
}
boolean eulersPrimePoly(float num) {
float lastSeenNum = 0;
int numsSeen = 0;
while (lastSeenNum < num) {
numsSeen++;
if (isPrime(sq(numsSeen)-numsSeen+41)) {
lastSeenNum = sq(numsSeen)-numsSeen+41;
}
}
return lastSeenNum == num;
}
boolean eulersPrimePolyOutliers(float num) {
float lastSeenNum = 0;
int numsSeen = 0;
while (lastSeenNum < num) {
numsSeen++;
if (!isPrime(sq(numsSeen)-numsSeen+41)) {
lastSeenNum = sq(numsSeen)-numsSeen+41;
}
}
return lastSeenNum == num;
}
boolean divisibleBy(float num, float denominator) {
return (num/denominator)%1 == 0;
}
void turn() {
if (dx == step) {
dx = 0;
dy = -step;
} else if (dy == -step) {
dx = -step;
dy = 0;
} else if (dx == -step) {
dx = 0;
dy = step;
} else if (dy == step) {
dx = step;
dy = 0;
}
}