-
Notifications
You must be signed in to change notification settings - Fork 1
/
Problem058_SpiralPrimes.java
66 lines (44 loc) · 1.34 KB
/
Problem058_SpiralPrimes.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
/**
* Created by Sneeuwpopsneeuw on 16-Aug-16.
*/
public class Problem058_SpiralPrimes extends Problem {
public static void main(String[] args) {
int ans = 0, i = 3, numbers = 1;
double precentage = 1D;
while (precentage >= 0.1D) {
int pow = (int)Math.pow(i, 2);
for(int j=0; j<=3; j++) {
if(isPrime( pow - (i-1)*j ) )
ans++;
numbers ++;
}
precentage = (double)ans/(double)numbers;
System.out.println( i+" : "+ans+"/"+numbers+" ~ "+precentage);
i += 2;
}
}
}
/*
__Same_solution_using_a_ArrayList_to_store_the_values__
public static void main() {
int ans = 0, i = 3;
ArrayList<Integer> map = new ArrayList<>();
double precentage = 1D;
map.add(1);
while (precentage >= 0.1D) { // the main loop should be changed
int pow = (int)Math.pow(i, 2);
map.add(pow);
for(int j=1; j<=3; j++) {
map.add( pow - (i-1)*j );
}
for(int j=map.size()-1; j>map.size()-4-1; j--) {
if(isPrime( map.get(j)) ) {
ans++;
}
}
precentage = (double)ans/(double)map.size();
System.out.println( i+" : "+ans+"/"+map.size()+" ~ "+ precentage );
i += 2;
}
}
*/