I have decided to start doing a few of the Project Euler (**PE) problems to help get my mind kicking again after a few days of being distracted by real life issues and picked one I haven't completed before: 27.
Euler discovered the remarkable quadratic formula:
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:
Now with every PE I come across I stop to think if there is any way of solving this problem without writing a single line of code. If you finish many of these problems and lurk through their forums you find posts from people on how to solve every one with assembly and where possible how to solve them with pen and paper.
Since we only need to check a maximum of four million possible equations we could probably do it by hand. To make it easier though you want to find any possible limitations where we know doesn't meet what we need.
In this case we want to find the maximum number of primes of consecutive n starting from n=0. So the earliest an equation will fail is if when n=0 the equation isn't a prime.
(0)² + a(0) + b
So no matter what we need b to be a prime. This greatly reduces the number of possible bs that we can use. A quick check gives me a count of 168 primes less than 1000; nearly a order of magnitude less combinations. Since primes can't be negative by definition ( this appears to be in debate on portions of the Internet so I'm going to go with the smaller set since both given quadratics always result in positive primes ) b is from this set of primes.
By the previous statement about the nature of primes a must always be at least as large as one less than -b. This I get from when n = 1 (the first n where a matters):
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 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:
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.n² + an + b, where |a| < 1000 and |b| < 1000
where |n| is the modulus/absolute value of ne.g. |11| = 11 and |-4| = 4
Now with every PE I come across I stop to think if there is any way of solving this problem without writing a single line of code. If you finish many of these problems and lurk through their forums you find posts from people on how to solve every one with assembly and where possible how to solve them with pen and paper.
Since we only need to check a maximum of four million possible equations we could probably do it by hand. To make it easier though you want to find any possible limitations where we know doesn't meet what we need.
In this case we want to find the maximum number of primes of consecutive n starting from n=0. So the earliest an equation will fail is if when n=0 the equation isn't a prime.
(0)² + a(0) + b
b
So no matter what we need b to be a prime. This greatly reduces the number of possible bs that we can use. A quick check gives me a count of 168 primes less than 1000; nearly a order of magnitude less combinations. Since primes can't be negative by definition ( this appears to be in debate on portions of the Internet so I'm going to go with the smaller set since both given quadratics always result in positive primes ) b is from this set of primes.
By the previous statement about the nature of primes a must always be at least as large as one less than -b. This I get from when n = 1 (the first n where a matters):
(1)² + a(1) + b
a + b + 1
Now the fact that they gave us an equation that is itself outside of the bounds of the coefficients stands out to me. This gives me the impression that there isn't an equation within the range that will ever result in a larger range than 80.
Now given this I am going to make an assumptions to help limit the runtime of my code that isn't necessarily true and I wouldn't use in a production that would require me to get the correct answer the first time 100%. The highest prime that we will reach will be less than 6400 (n**2 for the already assumed largest n).
From here it is a simple triple nested for loop to find the resulting coefficients and answer.
Sample python code for the solution with the hardcoded primes removed for viewability (or view at github):
large_primes = [ 2, ... 6421] primes = [ 2, ... 997 ] highest_count = 40 highest_blarg = 41 # 41*1 for x in primes: for y in xrange(-1*x-1,1000): for z in xrange( 80 ): if( z*z+y*z+x not in large_primes ): if( z > highest_count ): highest_count = z highest_blarg = x*y break print highest_blarg,":",highest_count
The solution if you don't want to plug it in yourself: -59231








