-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEuler27.java
More file actions
40 lines (35 loc) · 803 Bytes
/
Euler27.java
File metadata and controls
40 lines (35 loc) · 803 Bytes
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
package Euler;
public class Euler27 {
private static boolean isPrime(long n){
if(n % 2 == 0 && n != 2) return false;
int lim = (int) Math.sqrt(n);
for(int i = 3; i <= lim; i+=2){
if(n % i == 0) return false;
}
return true;
}
private static int ConsecPrimes(int a, int b){
for(int i = 0; ; i++){
int ans = (int) Math.pow(i, 2) + a*i + b;
if(ans < 0 || !isPrime(ans)) return i;
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
int bestN = 0;
int bestA = 0;
int bestB = 0;
for(int a = -1000; a < 1001; a++){
for(int b = -1000; b < 1001; b++){
int ans = ConsecPrimes(a,b);
if(ans > bestN){
bestN = ans;
bestA = a;
bestB = b;
}
}
}
int result = bestA * bestB;
System.out.println(result);
}
}