# Project Euler 27: Quadratic primes

Euler discovered the remarkable quadratic formula:

\[n^2 + n + 41\]

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

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

Considering quadratics of the form:

\[n^2 + an + b,\] where \(|a| < 1000\) and \(|b| \le 1000\) and 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\).

## Solution

Based on the problem statement, a very simple brute force algorithm can be stated, using the isPrime function from Problem 7:

function solution() { var maxC = 0; var maxAB = 0; for (var a = -999; a <= 999; a++) { for (var b = -1000; b <= 1000; b++) { var c = countConsecutivePrimes(a, b); if (c > maxC) { maxC = c; maxAB = a * b; } } } return maxAB; } function countConsecutivePrimes(a, b) { for (var n = 0; ; n++) { var t = n * n + a * n + b; if (!isPrime(t)) { return n; } } }

This sledgehammer method is quite fast already, but far from satisfying. Since \(n^2 + an + b\) must generate a series of primes, we can easily find optimizations to algorithm above. Starting with \(n_0:= 0\), the first element becomes

\[n_0^2+an_0+b = b\]

Since the quadratic equation must be prime for every element, it follows that \(b\) must be prime as well. For the next element, a similar step can be performed with \(n_1=1\):

\[n_1^2+an_1+b = 1 + a + b\]

Since \(b\) is prime, and all primes but 2 are odd, it follows that \(a\) must be odd for all \(b\neq 2\) and even for \(b= 2\). This lets us improve the algorithm as follows:

var primes = [ 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]; function solution() { var maxC = 0; var maxAB = 0; for (var a = -999; a <= 1001; a+= 2) { for (var i = 0; i < primes.length; i++) { var b = primes[i]; var c = countConsecutivePrimes(a - (i == 0 ? 1 : 0), b); if (c > maxC) { maxC = c; maxAB = a * b; } } } return maxAB; }