-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProblem27.java
62 lines (46 loc) · 1.15 KB
/
Problem27.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
package com.prog.ProjectEulerPrograms;
public class Problem27 {
public static void main(String[] args) {
int a, b, n, previousCount = 0, count = 1, answer = 0;
boolean check = false;
// log start time
long start = System.currentTimeMillis();
for(a = -999; a<1000; a++) {
for(b = 2; b<1000; b++) {
if(checkPrime(b)) {
for(n = 1; check==false; n++) {
if((n*n + a*n + b) > 0 && checkPrime(n*n + a*n + b)) {
count++;
}
else
check = true;
}
if(count > previousCount) {
previousCount = count;
answer = a*b;
}
}
count = 1;
check = false;
}
}
// log end time
long end = System.currentTimeMillis();
System.out.println("Answer is: "+answer);
System.out.println("Count is: "+previousCount);
System.out.println("Execution time in milliseconds: "+(end - start)+" ms");
}
private static boolean checkPrime(int n) {
boolean result = true;
if(n == 2 || n == 3)
result = true;
else {
for(int i = 2; i<= Math.sqrt(n); i++) {
if(n % i == 0)
result = false;
}
return result;
}
return result;
}
}