-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSolution27.java
More file actions
73 lines (72 loc) · 2.04 KB
/
Copy pathSolution27.java
File metadata and controls
73 lines (72 loc) · 2.04 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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
//import java.util.List;
//import java.util.ArrayList;
public class Solution27{
/*
static List<Integer> primeSieve(int n){
boolean[] primeCheck = new boolean[n+1];
List<Integer> primes = new ArrayList<Integer>();
for(int i = 0; i < n; i++){
primeCheck[i] = true;
}
for(int i = 2; i * i <= n; i++){
if(primeCheck[i]){
//Mark all multiples of i as non-primes.
for(int j = i * i; j <= n ; j += i){
primeCheck[j] = false;
}
}
}
for(int i = 2; i <= n; i++){
if(primeCheck[i]){
primes.add(i);
}
}
return primes;
}
*/
static boolean isPrime(int num){
if(num <= 1){
return false;
}
else if(num <= 3){
return true;
}
else if(num % 2 == 0 || num % 3 ==0){
return false;
}
else{
//Note that any primes greater than 2 or 3 can be expressed in the form 6k+1 of 6k - 1.
for(int i = 5; i <= Math.sqrt(num);i+=6){
//Check if num is divisible by 6k + 1 or 6k - 1.
if(num % i == 0 || num % (i + 2) == 0){
return false;
}
}
return true;
}
}
public static void main(String[] args) {
/*List<Integer> bVals = primeSieve(1000);
for(int a = -999; a < 1001; a += 2){
}
*/
//Need to work on efficient solution...
int nMax = 0;
int aMax = 0;
int bMax = 0;
for(int a = -999; a < 1000; a++){
for(int b = -1000; b <= 1000; b++){
int n = 0;
while (isPrime(Math.abs(n * n + a * n + b))) {
n++;
}
if (n > nMax) {
aMax = a;
bMax = b;
nMax = n;
}
}
}
System.out.println(aMax * bMax);
}
}