Problem 291: Panaitopol Primes

A prime number p is called a Panaitopol prime if p=dfrac{x^4-y^4}{x^3+y^3} for some positive integers x and y.

Find how many Panaitopol primes are less than 5 * 10^15.

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

It's easy to make a brute force search for all Panaitopol prime below 1000.
After about a minute my program displayed 5,13,41,61,113,181,313,421,613,761 (see isPanaitopolPrime and #define BRUTE_FORCE).
I entered this sequence into my favorite internet search engine and found immediately oeis.org/A027862

They claimed that each Panaitopol prime can be represented as n^2 + (n+1)^2.
All I did is copying my Miller-Rabin primality check from my toolbox and testing each numbercurrent = n*n + (n+1)*(n+1) whether it's prime.

It takes about 2 minutes on my machine which exceeds the inofficial time limit of Project Euler.
I saw a few different approaches which are much faster but I don't fully understand the mathematics
(and honestly I can't fully follow the reasoning why each Panaitopol prime is part of n^2 + (n+1)^2).

Note

mulmod was tuned for the 64-bit GCC compiler (on average about 4000 CPU cycles per test → 1 million tests per second / single-threaded).
Visual C++ is much slower.

In September 2017 I added OpenMP support. My 8-core computer finds the correct result in less than 20 seconds now.
However, the single-core performance remains unchanged and I still label my solution as "inefficient".

Interactive test

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

Benchmark

The correct solution to the original Project Euler problem was found in 121 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.
(compiled for x86_64 / Linux, GCC flags: -O3 -march=native -fno-exceptions -fno-rtti -std=gnu++11 -DORIGINAL)

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 !