So, for example, the fifth number with at least 5 prime factors is 80.

Find the millionth number with at least one million prime factors.
Give your answer modulo 123454321.

My Algorithm

Based on the example I figured out pretty soon that most of the "one million prime factors" will be 2s.
The one millionth prime number is 15485863, therefore the smallest number with at least one million prime factors cannot exceed 2^999999 * 15485863 (see tooLarge).

I assumed that all but the last 32 prime factors will be 2s (in fact I later found empirically that all but the last 27 must be 2s).
A minimum-priority-queue, that means smallest numbers first, is initialized with 2^32 (later changed to 2^27).
Then I pick and remove the smallest number current one million times and:

"append a prime": multiply current by p where p will be any of the first million primes - but this time I take care that p is no smaller than the largest prime factor of current

"replace a prime": replace one of the 2s of current by p where p will be any of the first million primes - but this time I take care that p is no smaller than the largest prime factor of current (same condition as above)

if the new numbers exceed the limit mentioned above, then I continue with the next number

After one million iterations the priority queue's top() element will be the smallest number with at least 32 (later reduced to 27) prime factors.

Multiplying that number 999968 times by 2 while repeatedly applying modulo 123454321 seems to be pretty slow
but GCC does an amazing job at optimizing this loop and it finished in a few milliseconds.

Note

The code was slightly modified after I submitted the correct result: initially, I had an std::set<Number> candidates which spent most of the time allocating (and freeing) memory.
The std::priority_queue is about twice as fast when its internal container is an std::vector.

Most of the one million primes will never be used so I looked for the smallest prime that still produces the correct result:
Starting with 173207, which is the 15770th prime, instead of 15485863 brings down the execution time to about one third of a second (see #define FAST).
I found that number solely by trial'n'error.

There is probably a smarter way to avoid the duplicates in candidates but I wasn't willing to spend much time on it.

Interactive test

This feature is not available for the current problem.

My code

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

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 !