问题描述:
Euler published the remarkable quadratic formula:
n² + n + 41
It turns out that the formula will produce 40 primes for the consecutive values n = 0 to 39. However, when n = 40, 402 + 40 + 41 = 40(40 + 1) + 41 is divisible by 41, and certainly when n = 41, 41² + 41 + 41 is clearly divisible by 41.
Using computers, the incredible formula n² 79n + 1601 was discovered, which produces 80 primes for the consecutive values n = 0 to 79. The product of the coefficients, 79 and 1601, is 126479.
Considering quadratics of the form:
解决问题:
利用题目本身的限制条件,因为a和b是1000以下,那么先使用boolean[] prime = new boolean[1000001];
public static boolean IsPrime(int number){ boolean result = true; if(number%2==0){ result = false; }else{ int middle = (int)Math.sqrt(number); for(int i=3; i<=middle; i+=2){ if(number%i==0){ result = false; break; } } } return result; } public static void find(){ int a =0, b=0; int max_len = 0; //先找出1000以内的所有素数 boolean[] prime = new boolean[1000001]; Arrays.fill(prime, false); for(int i=2;i<=1000001;i++){ if(IsPrime(i)){ prime[i] = true; } } for(int i=-1000; i<=1000; i++){ //循环a for(int j=-1000; j<=1000; j++){ //循环b int n; for( n=0; n<Math.abs(j); n++){ int value = n*n+ i*n + j; if(value>0&&prime[value]){ ; }else{ break; } } if(max_len<n){ a = i; b = j; max_len = n; } } } System.out.println("a:"+a+",b:"+b+",len"+max_len); System.out.println(a*b); }