Problem 27 : Quadratic primes
Euler discovered the remarkable quadratic formula:
n2+n+41
It turns out that the formula will produce 40 primes for the consecutive integer values 0≤n≤39. However, when n=40, 402+40+41=40(40+1)+41 is divisible by 41, and certainly when n=41, 412+41+41 is clearly divisible by 41.
The incredible formula n2−79n+1601 was discovered, which produces 80 primes for the consecutive values 0≤n≤79. The product of the coefficients, −79 and 1601, is −126479.
Considering quadratics of the form:
n2+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.
C++ source code
#include <iostream>
#include <cmath> // sqrtl()
#include <cassert> // assert()
using namespace std;
class PE0027
{
private:
int m_a;
int m_b;
int m_n;
bool checkPrime(long long number);
public:
bool checkConsecutivePrimes(int a, int b, int N);
void producesMaxConsecutivePrimes();
};
bool PE0027::checkPrime(long long number)
{
long long i;
long double squareRoot=sqrtl((long double)number);
if (number < 2 || number%2 == 0 && number!=2)
{
return false;
}
for(i=3;i<=(long long)squareRoot;i+=2) // 3, 5, 7, ...
{
if (number % i == 0)
{
return false;
}
}
return true;
}
bool PE0027::checkConsecutivePrimes(int a, int b, int N)
{
long long number;
for(int n=0; n<N; n++)
{
number = n*n + a*n + b; // n^2 + n + 41
if (false == checkPrime(number))
{
break;
}
}
return true;
}
void PE0027::producesMaxConsecutivePrimes()
{
long long number;
int a, b, n;
m_a = m_b = m_n = 0;
for(a=-999; a<1000; a++) // |a|<1000|
{
for(b=-1000; b<=1000; b++) // |b|<=1000
{
for(n=0; ;n++) // n is numOfPrimes
{
number = n*n + a*n + b; // e.g. n^2 + n + 41
if (false == checkPrime(number))
{
break;
}
}
if (n > m_n)
{
m_a = a;
m_b = b;
m_n = n;
}
}
}
if (m_a < 0)
{
cout << "The quadratic formula n^2 - " << -m_a;
}
else
{
cout << "The quadratic formula n^2 + " << m_a;
}
if (m_b < 0)
{
cout << "n - " << -m_b;
}
else
{
cout << "n + " << m_b;
}
cout << ", produces " << m_n << " consecutive primes." << endl;
cout << "The product of the coefficients, a and b, is " << m_a*m_b << endl;
}
int main()
{
PE0027 pe0027;
assert(true == pe0027.checkConsecutivePrimes(1, 41, 40));
pe0027.producesMaxConsecutivePrimes();
return 0;
}
Python source code
import math
def checkPrime(x):
if x < 2 or x%2 == 0 and x!=2:
return False
for i in range(3, int(math.sqrt(x))+1, 2): # 3, 5, 7, ...
if x % i == 0:
return False
return True
def checkConsecutivePrimes(a, b, N):
for n in range(0, N):
number = n*n + a*n + b # n^2 + n + 41
if False == checkPrime(number):
break
return True
def producesMaxConsecutivePrimes():
m_a = m_b = m_n = 0
for a in range(-999, 1000): # |a|<1000|
for b in range(-1000, 1001): # |b|<=1000
n = 0 # n is numOfPrimes
while True:
number = n*n + a*n + b # e.g. n^2 + n + 41
if False == checkPrime(number):
break
n += 1
if n > m_n:
m_a = a;
m_b = b;
m_n = n;
if m_a < 0:
print("The quadratic formula n^2 -",-m_a,end=' ')
else:
print("The quadratic formula n^2 +",m_a,end=' ')
if m_b < 0:
print("n-",-m_b,end=' ')
else:
print("n+",m_b,end=' ')
print(", produces",m_n,"consecutive primes.")
print("The product of the coefficients, a and b, is",m_a*m_b)
def main():
assert True == checkConsecutivePrimes(1, 41, 40)
producesMaxConsecutivePrimes()
if __name__ == '__main__':
main()