-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathProblem_027.java
More file actions
45 lines (36 loc) · 1 KB
/
Problem_027.java
File metadata and controls
45 lines (36 loc) · 1 KB
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
package euler;
public class Problem_027 {
static int quadraticPrimeProduct() {
int product = 0;
int mostConsecutivePrimes = 0;
for(int a = -999; a < 1000; a++) {
for(int b = -1000; b < 1001; b++) {
int consecPrimes = consecutivePrimes(a, b);
if(mostConsecutivePrimes < consecPrimes) {
mostConsecutivePrimes = consecPrimes;
product = a * b;
// System.out.println(mostConsecutivePrimes + " : " + a + " * " + b + " = " + product);
}
}
}
return product;
}
private static int consecutivePrimes(int a, int b) {
int primes = 0, n = 0;
boolean isConsecutive = true;
while(isConsecutive) {
isConsecutive = isPrime((n*n) + (a*n) + b);
n++;
primes++;
}
return primes - 1;
}
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;
}
}