Problem 110: Diophantine reciprocals II

It can be verified that when n = 1260 there are 113 distinct solutions and this is the least value of n
for which the total number of distinct solutions exceeds one hundred.

What is the least value of n for which the number of distinct solutions exceeds four million?

NOTE: This problem is a much more difficult version of Problem 108 and as it is well beyond the limitations of a brute force approach it requires a clever implementation.

My Algorithm

As mentioned in my solution for problem 108, a prime factorization can find the number of solutions.
This time I "invert" the algorithm: I generate prime factorizations in ascending order.
If e_0, e_1, e_2, ... are the exponents of the first primes 2, 3, 5, ... then:value = 2^{e_0} * 3^{e_1} * 5^{e_2} ...
The container exponents stores these exponents, while prime obviously holds the prime numbers.
Empirically I found that the first 12 prime numbers (2 ... 37) are sufficient for the original problem.

This gives ((2e_0 + 1)(2e_1 + 1)(2e_2 + 1) ... + 1) factorizations. However, x and y are interchangeable, therefore I must divide the result by 2 as well.

A nice property of std::map is that its elements are always sorted by their keys.
Therefore todo.begin() always refers to the smallest unprocessed number.
If it has too few prime factorizations then I increment its components and re-insert them into todo.

Hackerrank has a broader input ranges which requires a few more prime numbers.
Even more, the result may be too big for 64 bit integer. That's why I opt for long double instead.
The failed test case is probably due to exceeding the precision of long double.

Note

I noticed that the exponents of larger prime numbers are at most 1 (when processing numbers <= 4000000).
My code runs about 3x faster and needs less memory when I avoid incrementing e_4, e_5, ...

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 100 | ./110

Output:

(please click 'Go !')

Note: the original problem's input 4000000cannot 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.

The code contains #ifdefs to switch between the original problem and the Hackerrank version.Enable #ifdef ORIGINAL to produce the result for the original problem (default setting for most problems).

#include<iostream>

#include<iomanip>

#include<vector>

#include<map>

intmain()

{

unsignedlonglong limit = 4000000;

std::cin>> limit;

#define ORIGINAL

#ifdef ORIGINAL

// 12 primes are enough for the original problem, I added a few more for the Hackerrank version

Hackerrank

Difficulty

40%
Project Euler ranks this problem at 40% (out of 100%).

Hackerrank describes this problem as easy.

Note:Hackerrank has strict execution time limits (typically 2 seconds for C++ code) and often a much wider input range than the original problem.In my opinion, Hackerrank's modified problems are usually a lot harder to solve. As a rule thumb: brute-force is rarely an option.

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 !