-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathQuestion047.java
60 lines (54 loc) · 973 Bytes
/
Question047.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
package euler;
import java.util.ArrayList;
public class Question047
{
public static void main(String[] args)
{
ArrayList<Integer> list = new ArrayList<Integer>();
list.add(0); // 2
list.add(0); // 3
list.add(1); // 4
ArrayList<Integer> check = new ArrayList<Integer>();
int length = 4;
for (int i = 0; i < length; i++)
{
check.add(length);
}
for (int i = 5; ; i++)
{
list.add(countPrime(i));
if (list.equals(check))
{
System.out.println(i - length + 1);
break;
}
list.remove(0);
}
}
private static int countPrime(int n)
{
int count = 0;
for (int i = 2; i < n; i += 1)
{
if (n % i == 0)
{
if (testPrime(i))
{
count++;
}
}
}
return count;
}
private static boolean testPrime(long i)
{
for (long j = 2l; j < (int)Math.sqrt(i) + 1; j++)
{
if (i % j == 0)
{
return false;
}
}
return true;
}
}