Tim Varley Logo
Tim Varley Systems Engineer
Problem #27 hard

Quadratic Primes

Euler discovered the remarkable quadratic formula:

n2+n+41n^2 + n + 41

It turns out that the formula will produce 4040 primes for the consecutive integer values 0n390 \le n \le 39. However, when n=40,402+40+41=40(40+1)+41n = 40, 40^2 + 40 + 41 = 40(40 + 1) + 41 is divisible by 4141, and certainly when n=41,412+41+41n = 41, 41^2 + 41 + 41 is clearly divisible by 4141.

The incredible formula n279n+1601n^2 - 79n + 1601 was discovered, which produces 8080 primes for the consecutive values 0n790 \le n \le 79. The product of the coefficients, 79-79 and 16011601, is 126479-126479.

Considering quadratics of the form:

n2+an+bn^2 + an + b, where a<1000|a| < 1000 and b1000|b| \le 1000

where n|n| is the modulus/absolute value of nn
e.g. 11=11|11| = 11 and 4=4|-4| = 4

Find the product of the coefficients, aa and bb, for the quadratic expression that produces the maximum number of primes for consecutive values of nn, starting with n=0n = 0.

Implementations

cpp
// https://projecteuler.net/problem=27
// Euler discovered 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 = 41² = 1681 is divisible by 41,
// and certainly when n = 41, 41² + 41 + 41 is clearly divisible by 41.
//
// 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² + a*n + 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.
//
// Answer: -59231
// Authored by: Tim Varley 💘
// Assisted-by: Grok Code Fast via Crush 💘 <crush@charm.land>
#include <iostream>
#include <cmath>
bool is_prime(long long n) {
if (n <= 1) return false;
if (n <= 3) return true;
if (n % 2 == 0 || n % 3 == 0) return false;
for (long long i = 5; i * i <= n; i += 6) {
if (n % i == 0 || n % (i + 2) == 0) return false;
}
return true;
}
int quadratic_primes_max_product(int max_a, int max_b) {
int max_streak = 0;
int best_product = 0;
for (int a = -max_a + 1; a < max_a; ++a) {
for (int b = 2; b <= max_b; ++b) { // b must be prime
if (!is_prime(b)) continue;
int n = 0;
while (true) {
long long val = (long long)n * n + a * n + b;
if (!is_prime(val)) break;
++n;
}
if (n > max_streak) {
max_streak = n;
best_product = a * b;
}
}
}
return best_product;
}
#if ! defined UNITTEST_MODE
int main(int argc, char const *argv[]) {
std::cout << quadratic_primes_max_product(1000, 1000) << std::endl;
}
#endif // UNITTEST_MODE

Solution Notes

Mathematical Background

This problem explores quadratic polynomials that generate prime numbers for consecutive integer inputs. Euler’s remarkable formula n2+n+41n^2 + n + 41 produces primes for n=0n = 0 to 3939, while the more impressive n279n+1601n^2 - 79n + 1601 generates 8080 consecutive primes. The challenge requires finding coefficients aa and bb in the general quadratic n2+an+bn^2 + an + b that maximize the length of such prime-generating sequences, subject to the constraints a<1000|a| < 1000 and b1000|b| \le 1000.

Algorithm Analysis

The solution employs a brute-force approach, iterating over all possible coefficient pairs within the given bounds. For each pair (a,b)(a, b), it evaluates the quadratic formula for consecutive nn starting from 00 until a non-prime value is encountered. Primality testing uses trial division up to n\sqrt{n}, optimized with the 6k±16k\pm1 sieve pattern for efficiency. The algorithm’s time complexity is O(A×B×N×P)O(A \times B \times N \times \sqrt{P}), where AA and BB are the coefficient ranges (approximately 2×1062 \times 10^6 iterations), NN is the maximum prime streak length (around 8080), and PP is the largest quadratic value tested (up to 10610^6).

Key Insights

For n=0n=0, the quadratic evaluates to bb, requiring bb to be prime and positive (since negative primes aren’t considered in this context). For n=1n=1, the expression becomes 1+a+b1 + a + b, which must also be prime. The optimal solution produces 7171 consecutive primes with coefficients a=61a = -61 and b=971b = 971, yielding the product 59231-59231.

Educational Value

This problem demonstrates the application of number theory in computational algorithms, combining prime generation with polynomial evaluation. It teaches efficient primality testing, brute-force optimization techniques, and the importance of mathematical constraints in algorithm design. The solution highlights how seemingly simple mathematical formulas can lead to complex computational challenges requiring careful analysis of edge cases and performance considerations.