-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathProblem_058.java
More file actions
33 lines (27 loc) · 885 Bytes
/
Problem_058.java
File metadata and controls
33 lines (27 loc) · 885 Bytes
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
package euler;
public class Problem_058 {
static int spiralPrimes() {
return spiralPrimes(0.1f);
}
private static int spiralPrimes(float pThresh) {
int numPrimes = 0;
int total = 0;
int i = 3, gap = 1;
while ((float) numPrimes / total >= pThresh || Float.compare((float) numPrimes / total, Float.NaN) == 0) {
if(Math.sqrt(i) - (int) Math.sqrt(i) == 0)
gap = (int) Math.sqrt(i);
if (isPrime(i)) numPrimes++;
total++;
i += gap + 1;
}
return gap;
}
private static boolean isPrime(long num) {
if (num < 2) return false;
if (num == 2) return true;
if (num % 2 == 0) return false;
for (long i = 3; i * i <= num; i += 2)
if (num % i == 0) return false;
return true;
}
}