Efficient code for minimum integer with given number of factors
Note that the divisor count function of a number with prime factorization $$n=p_1^{a_1} p_2^{a_2} \cdots p_i^{a_i}$$ satisfies: $$\tau (n)=\prod _k^i \left(a_k+1\right)$$
So, to find an inverse of the divisor count function, we need to find a number whose prime factorization is equal to the right hand side, from which we can determine what the values of $a_k$ must be. Here is a function that does this:
InverseDivisor[n_] := With[
{f = Reverse[Join @@ ConstantArray @@@ FactorInteger[n] -1]},
Times @@ ((Prime @ Range @ Length @ f)^f)
]
Some more work is needed to make sure the inverse returned is the minimum. For example, my simple minded algorithm gives 30 instead of 24 for the inverse of 8. Let's check your examples:
i = InverseDivisor /@ Range[9]
{1, 2, 4, 6, 16, 12, 64, 30, 36}
And let's check your harder version:
InverseDivisor[60]
5040
Next, let's see how long it takes to do $10^6$:
big = InverseDivisor[10^6]; //AbsoluteTiming
big
{0.000059, Null}
200961610708938459249870000
Finally, we can check the above results by using DivisorSigma
:
DivisorSigma[0, i]
DivisorSigma[0, 5040]
DivisorSigma[0, big]
{1, 2, 3, 4, 5, 6, 7, 8, 9}
60
1000000
If a number $n$ has $k$ divisors, then $k=(a_1+1)(a_2+1)\ldots (a_m+1)$, where the prime factorisation of $n=p_1^{a_1} p_2^{a_2}\ldots p_m^{a_m}$, as @CarlWoll points out. Therefore, $k$ must be the product of $m$ factors, each $\ge2$.
The general problem of multiplicative partitions is discussed on Stack Exchange here and here. Adapting code from the article by Knopfmacher and Mays gives the following function MultiplicativePartitions[n]
.
MultiplicativePartitions[1, m_] := {{}}
MultiplicativePartitions[n_, 1] := {{}}
MultiplicativePartitions[n_?PrimeQ, m_] := If[m < n, {}, {{n}}]
MultiplicativePartitions[n_, m_] :=
Join @@ Table[
Map[Prepend[#, d] &, MultiplicativePartitions[n/d, d]],
{d, Select[Rest[Divisors[n]], # <= m &]}]
MultiplicativePartitions[n_] := MultiplicativePartitions[n, n]
For example,
MultiplicativePartitions[24]
{{3, 2, 2, 2}, {4, 3, 2}, {6, 2, 2}, {6, 4}, {8, 3}, {12, 2}, {24}}
Thus,
MinWithDivisors[k_] :=
Min[Map[Times @@ (Prime[Range[Length[#]]]^(# - 1)) &,
MultiplicativePartitions[k]]]
SetAttributes[MinWithDivisors, Listable]
A quick test:
MinWithDivisors[Range[20]]
{1, 2, 4, 6, 16, 12, 64, 24, 36, 48, 1024, 60, 4096, 192, 144, 120, 65536, 180, 262144, 240}
The function MinWithDivisors[k]
agrees with a brute-force search.
Block[{t = DivisorSigma[0, Range[300000]]},
Table[FirstPosition[t, k, {0}][[1]], {k, 1, 20}]
]
The solution for one million divisors, $k=10^6$, is the following.
AbsoluteTiming[MinWithDivisors[10^6]]
{0.077611, 173804636288811640432320000}
Note that this result is less than that given by InverseDivisor
defined by Carl, who correctly warned that "more work" was required.