From Project Euler problem 500
The number of divisors of 120 is 16. In fact 120 is the smallest number having 16 divisors.
Find the smalle
You should use the formula for the number of divisors of integer n:
d(n) = (a1+1)(a2+1)...(ak+1)
where
n = p1a1 * p2a2 *p3a3 *...*pkak
is a unique representation of every integer through powers of its prime divisors. This is a well-known formula, but if one wonders how to get it, note that d divides n if and only if d is of the form p1x1 * p2x2 *p3x3 *...*pkxk, where each of xi is between 0 and ai, so there are ai + 1 possibilities for choosing each of xi. Now just apply the product rule and you get the desired formula.
For fixed d(n) (as in your case), the minimum value of n is obviously obtained by carefully selecting powers of existing primes, or by adding new primes. Let's work through this simple example, 16:
d(x) = (a1+1)(a2+1)...(ak+1) = 16 = 24.
This means that you have at most 4 different primes, therefore:
x = 2a1 * 3a2 *5a3 * 7a4
where ai >= 0. The question is now - in order to get minimum value of x, is it better to increase the powers of 2 (i.e., increment a1), or to use 7 (i.e. take a4=1 instead of a4=0)? Well, it's simple to check, 2*3*5*7 > 23 * 3 * 5 = 120, that's why the 120 is answer in this case.
How to generalize this approach? You should create min-heap where you'll put the powers of primes, taking care that the number of divisors reaches the specified value. In case of 16, this min-heap would contain numbers 2, 3, 5, 7, 22, 32, 24 etc. Why? Because 16 = 24, so each of (ai+1) has to divide 16, i.e. it has to be power of 2. Every time when you add new power, it should increase the left hand side (i.e., the variable d(x)) by power of 2, since your final goal is to find the smallest number with 2500500 divisors. Heap is initialized with first k primes (in the problem statement, k = 500500), and in each step, when you pop px from the heap, p2x is returned and result is multiplied by px. Step-by-step solution for d(x) = 16 = 24:
Step Heap d(x) x
==========================
0 2,3,5,7 1 1
1 3,4,5,7 2 2
2 4,5,7,9 4 6
3 5,7,9,16 8 24
4 7,9,16,25 16 120
HTH.