[Project Euler] Problem 27
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:
n² + an + b, where |a| 1000 and |b| 1000
where |n| is the modulus/absolute value of n
e.g. |11| = 11 and |4| = 4
Find the product of the coefficients, a and b, for the quadratic expression that produces the maximum number of primes for consecutive values of n, starting with n = 0.
只说一点,b一定是正素数
因为当n为0时,表达式的值为b
直接上代码
#include <iostream>
#include <cmath>
usingnamespace std;
int prime[168] = {2,3,5,7,11,13,17,19,23,29,31,37,41,
43,47,53,59,61,67,71,73,79,83,89,97,
101,103,107,109,113,127,131,137,139,
149,151,157,163,167,173,179,181,191,
193,197,199,211,223,227,229,233,239,
241,251,257,263,269,271,277,281,283,
293,307,311,313,317,331,337,347,349,
353,359,367,373,379,383,389,397,401,
409,419,421,431,433,439,443,449,457,
461,463,467,479,487,491,499,503,509,
521,523,541,547,557,563,569,571,577,
587,593,599,601,607,613,617,619,631,
641,643,647,653,659,661,673,677,683,
691,701,709,719,727,733,739,743,751,
757,761,769,773,787,797,809,811,821,
823,827,829,839,853,857,859,863,877,
881,883,887,907,911,919,929,937,941,
947,953,967,971,977,983,991,997};
bool isPrime(int a);
int getLength(int i,int a[]);
int main(){
int a[168];
int length[168];
int max =0;
int tmp;
for(int i=0; i<168; i++){
length[i] = getLength(i,a);
}
for(int i=0; i<168; i++){
if(length[i] > max){
max = length[i];
tmp = i;
}
}
cout <<"a="<< a[tmp] <<"\tb="<< prime[tmp] <<"\tlength="<< length[tmp] << endl;
cout <<"product="<< a[tmp]*prime[tmp] << endl;
return0;
}
bool isPrime(int a){
bool tmp =true;
int i =0;
while(prime[i] <= sqrt(a)){
if(a%prime[i] ==0){
tmp =false;
break;
}
i++;
}
return tmp;
}
int getLength(int i,int a[]){
int length =0;
int at =0;
for(int j=-999; j<999; j++){
int k =0;
int tmp =0;
while((k*k+j*k+prime[i]) >0&& isPrime(k*k+j*k+prime[i])){
tmp++;
k++;
}
if(tmp > length){
at = j;
length = tmp;
}
}
a[i] = at;
return length;
}
#include <cmath>
usingnamespace std;
int prime[168] = {2,3,5,7,11,13,17,19,23,29,31,37,41,
43,47,53,59,61,67,71,73,79,83,89,97,
101,103,107,109,113,127,131,137,139,
149,151,157,163,167,173,179,181,191,
193,197,199,211,223,227,229,233,239,
241,251,257,263,269,271,277,281,283,
293,307,311,313,317,331,337,347,349,
353,359,367,373,379,383,389,397,401,
409,419,421,431,433,439,443,449,457,
461,463,467,479,487,491,499,503,509,
521,523,541,547,557,563,569,571,577,
587,593,599,601,607,613,617,619,631,
641,643,647,653,659,661,673,677,683,
691,701,709,719,727,733,739,743,751,
757,761,769,773,787,797,809,811,821,
823,827,829,839,853,857,859,863,877,
881,883,887,907,911,919,929,937,941,
947,953,967,971,977,983,991,997};
bool isPrime(int a);
int getLength(int i,int a[]);
int main(){
int a[168];
int length[168];
int max =0;
int tmp;
for(int i=0; i<168; i++){
length[i] = getLength(i,a);
}
for(int i=0; i<168; i++){
if(length[i] > max){
max = length[i];
tmp = i;
}
}
cout <<"a="<< a[tmp] <<"\tb="<< prime[tmp] <<"\tlength="<< length[tmp] << endl;
cout <<"product="<< a[tmp]*prime[tmp] << endl;
return0;
}
bool isPrime(int a){
bool tmp =true;
int i =0;
while(prime[i] <= sqrt(a)){
if(a%prime[i] ==0){
tmp =false;
break;
}
i++;
}
return tmp;
}
int getLength(int i,int a[]){
int length =0;
int at =0;
for(int j=-999; j<999; j++){
int k =0;
int tmp =0;
while((k*k+j*k+prime[i]) >0&& isPrime(k*k+j*k+prime[i])){
tmp++;
k++;
}
if(tmp > length){
at = j;
length = tmp;
}
}
a[i] = at;
return length;
}
运行结果: