Problem 216: Investigating the primality of numbers of the form 2n^2-1

Consider numbers t(n) of the form t(n) = 2n^2-1 with n > 1.
The first such numbers are 7, 17, 31, 49, 71, 97, 127 and 161.
It turns out that only 49 = 7 * 7 and 161 = 7 * 23 are not prime.
For n <= 10000 there are 2202 numbers t(n) that are prime.

How many numbers t(n) are prime for n <= 50,000,000 ?

Very inefficient solution

My code needs more than 60 seconds to find the correct result. (scroll down to the benchmark section)
Apparantly a much smarter algorithm exists - or my implementation is just inefficient.

My Algorithm

The Miller-Rabin primality test from my toolbox can easily solve this problem.
The only drawback: it takes xyz minutes.

In fact, if t(n) is a multiple of such k then not only t(n + k) but also t(n + 2k) and t(n + 3k) and so on are multiples of k.
And that means they can't be prime - reducing the number of Miller-Rabin tests to about one fifth.

I don't have a fast factorization algorithm in my toolbox yet, so I perform simple trial division to find prime factors.
After trying several values I found that it doesn't pay off to check for prime factors larger than one million (see MaxSievePrime).
Even more, I hardly found any candidates above two millions that are divisible by a prime which wasn't already a factor of a smaller candidates (see FilterThreshold).
Both constants were heuristically determined by lots of trial'n'error.

Alternative Approaches

The Tonelli-Shanks algorithm (which I wasn't aware of) is much faster.
I probably should translate Wikipedia's pseudo-code to C++ and add it to my toolbox.

Note

OpenMP gives a nice speed-up but I still need about 6 minutes to find the correct result (see #define PARALLEL).
The single-thread version finishes after about 33 minutes.
By the way: if I would use only my Miller-Rabin test (without the optimizations mentioned above), the program finishes after 55 minutes.

Reading the forums, the vast majority of solvers seem to have a simple loop invoking the prime test available in Java, Mathematica, etc.
They neither wrote the prime test nor looked for optimizations.
In my opinion, this is a quite hard problem if you really want to stick to the "one-minute rule".

Looking at the high number of solvers and the low difficulty rating I expected that I missed something big - but actually only a small number of people
discovered/knew the most appropriate way to solve this problem, the Tonelli-Shanks algorithm.

Interactive test

You can submit your own input to my program and it will be instantly processed at my server:

Input data (separated by spaces or newlines):

This is equivalent toecho 10000 | ./216

Output:

(please click 'Go !')

Note: the original problem's input 50000000cannot be enteredbecause just copying results is a soft skill reserved for idiots.

(this interactive test is still under development, computations will be aborted after one second)

My code

… was written in C++11 and can be compiled with G++, Clang++, Visual C++. You can download it, too.

Benchmark

The correct solution to the original Project Euler problem was found in 1960 seconds (exceeding the limit of 60 seconds).The code can be accelerated with OpenMP but the timings refer to the single-threaded version on an Intel® Core™ i7-2600K CPU @ 3.40GHz.
Peak memory usage was about 9 MByte.

Those links are just an unordered selection of source code I found with a semi-automatic search script on Google/Bing/GitHub/whatever.
You will probably stumble upon better solutions when searching on your own.
Maybe not all linked resources produce the correct result and/or exceed time/memory limits.

Heatmap

Please click on a problem's number to open my solution to that problem:

green

solutions solve the original Project Euler problem and have a perfect score of 100% at Hackerrank, too

yellow

solutions score less than 100% at Hackerrank (but still solve the original problem easily)

gray

problems are already solved but I haven't published my solution yet

blue

solutions are relevant for Project Euler only: there wasn't a Hackerrank version of it (at the time I solved it) or it differed too much

orange

problems are solved but exceed the time limit of one minute or the memory limit of 256 MByte

red

problems are not solved yet but I wrote a simulation to approximate the result or verified at least the given example - usually I sketched a few ideas, too

black

problems are solved but access to the solution is blocked for a few days until the next problem is published

[new]

the flashing problem is the one I solved most recently

I stopped working on Project Euler problems around the time they released 617.

The 310 solved problems (that's level 12) had an average difficulty of 32.6&percnt; at Project Euler and
I scored 13526 points (out of 15700 possible points, top rank was 17 out of &approx;60000 in August 2017)
at Hackerrank's Project Euler+.

My username at Project Euler is stephanbrumme while it's stbrumme at Hackerrank.

Copyright

I hope you enjoy my code and learn something - or give me feedback how I can improve my solutions.All of my solutions can be used for any purpose and I am in no way liable for any damages caused.You can even remove my name and claim it's yours. But then you shall burn in hell.

The problems and most of the problems' images were created by Project Euler.Thanks for all their endless effort !!!

more about me can be found on my homepage,
especially in my coding blog.
some names mentioned on this site may be trademarks of their respective owners.
thanks to the KaTeX team for their great typesetting library !