-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProblem058.java
More file actions
31 lines (31 loc) · 845 Bytes
/
Problem058.java
File metadata and controls
31 lines (31 loc) · 845 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
public class Problem058 {
public static boolean isPrime(int i) {
for (int j = 2; j <= Math.sqrt(i); j++) {
if (i % j == 0) {
return false;
}
}
return true;
}
public static void main(String[] args) {
double primes = 0;
double total = 1;
int layer = 1;
int last = 1;
System.out.println(primes/total);
do {
for (int i = 0; i < 4; i++) {
last += layer * 2;
total++;
if (isPrime(last)) {
primes++;
}
}
if (layer % 1000 == 0) {
System.out.println(primes / total);
}
layer++;
} while (primes / total > .1);
System.out.println(1 + 2 * (layer - 1));
}
}