# Efficient code for minimum integer with given number of factors

I'm seeking an efficient implementation of the number-theoretic function giving the smallest integer $$n$$ that has exactly $$k$$ factors (not necessarily prime):

f[k_Integer]:= ...

• f = 1 because $$1$$ is the smallest integer that has just a single factor, i.e., $$\{ 1 \}$$
• f = 2 because $$2$$ is the smallest integer that has just the two factors, i.e., $$\{ 1, 2 \}$$
• f = 4 because $$4$$ is the smallest integer that has exactly three factors, i.e., $$\{ 1, 2, 4 \}$$
• f = 6 because $$6$$ is the smallest integer that has exactly four factors, i.e., $$\{ 1, 2, 3, 6 \}$$
• f = 16 because $$16$$ is the smallest integer that has exactly five factors, i.e., $$\{ 1, 2, 4, 8, 16 \}$$
• f = 12 because $$12$$ is the smallest integer that has exactly six factors, i.e., $$\{ 1, 2, 3, 4, 6, 12 \}$$
• f = 64 because $$64$$ is the smallest integer that has exactly seven factors, i.e., $$\{ 1, 2, 4, 8, 16, 32, 64 \}$$
• f = 24 because $$24$$ is the smallest integer that has exactly eight factors, i.e., $$\{ 1, 2, 3, 4, 6, 8, 12, 24 \}$$
• f = 36 because $$36$$ is the smallest integer that has exactly nine factors, i.e., $$\{ 1, 2, 3, 4, 6, 9, 12, 18, 36 \}$$

A few moments of thought will show that for $$k$$ odd, $$n$$ is a perfect square. Moreover, note that f[k] is not monotonic.

Very inefficient code would advance through increasing $$n$$ until an integer is found with the criterion of exactly $$k$$ factors, but this is extremely inefficient for large $$k$$.

This generates the pairs $$n,k$$ up to $$n=100$$:

myList = Table[{n, Times @@ (# + 1 & /@ FactorInteger[n][[All, 2]])},
{n, 2, 100}]


And it is a simple matter to select cases with a given $$k$$:

Select[myList, #[] == 60]


When $$k \sim 10^6$$, this is somewhat slow and definitely memory intensive.

As background/edification, here is a log plot of $$n$$ versus $$k$$. In 1644, the great mathematician Mersenne asked for f = 5040.

• f = 5040 {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 12, 14, 15, 16, 18, 20, 21, 24, 28, 30, 35, 36, 40, 42, 45, 48, 56, 60, 63, 70, 72, 80, 84, 90, 105, 112, 120, 126, 140, 144, 168, 180, 210, 240, 252, 280, 315, 336, 360, 420, 504, 560, 630, 720, 840, 1008, 1260, 1680, 2520, 5040} is surprisingly fast. – bbgodfrey Nov 21 '18 at 3:12
• Yes... but I would like to calculate f[10^6] and even higher, without having to calculate billions of "lower" cases. – David G. Stork Nov 21 '18 at 3:15
• Understand, By the way, Table[f[k], {k, 1, 20, 1}] is {1, 2, 4, 6, 16, 12, 64, 24, 36, 48, 1024, 60, 4096, 192, 144, 120, 65536, 180, 262144, 240}. Check your f. – bbgodfrey Nov 21 '18 at 3:20
• @bbgodfrey. Oooh... thanks. Fixed. I did the table by hand before I wrote my code. (Bad idea!) – David G. Stork Nov 21 '18 at 3:23
• The accepted answer fails for ALL extraordinary numbers {8, 16, 24, 32, 48, 64, 72, 80, 96, 108...} and there are infinitely many of them oeis.org/A072066. An answer should be accepted only if it returns the correct results. – J42161217 Nov 21 '18 at 12:44

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


{1, 2, 4, 6, 16, 12, 64, 30, 36}

And let's check your harder version:

InverseDivisor


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

• Thanks so much (+1). I'm a bit surprised how fast this is for large $k$. Now all we need do is ensure that the minimum $n$ be returned. – David G. Stork Nov 21 '18 at 5:03
• @DavidG.Stork Consider why the 8 case fails. 8 can be represented by {{2, 3}}, which yields f = {1,1,1} and InverseDivisor of 30. However, 8 also can be represented by {{2, 1}, {4, 1}}, which yields f = {3,1} and InverseDivisor of 24. Generalizing, it appears that not only must the IntegerFactors of 'n be considered but also products of those factors: Analyze all sets of products of factors and chose the one that yields the smallest InverseDivisor. Though slower than the approach in the answer, it is vastly faster than the brute force approach in the question. – bbgodfrey Nov 21 '18 at 6:23
• @DavidG.Stork More generally, it appears that most cases that are multiples of 8 require the approach outlined in my comment immediately above. I have not found any other cases requiring that approach. – bbgodfrey Nov 21 '18 at 6:40
• @CarlWoll: Your code is fast enough (accept). (I guess I was too pessimistic in my predictions of how slow "basic" approaches would be.) – David G. Stork Nov 21 '18 at 6:50
• @DavidG.Stork I cannot understand how an aswer returning false results gets accepted... This code (which is very similar with OEIS code oeis.org/A037019) returns the right answer "most of the times". In number theory this makes a big difference and it is totally wrong. Here is the OEIS with correct results oeis.org/A005179. – J42161217 Nov 21 '18 at 11:06

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


{{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]


{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]},
Table[FirstPosition[t, k, {0}][], {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.

• Excellent! +1... – J42161217 Nov 21 '18 at 19:05
• @KennyColnago: Very nice (+1)... and reasonable efficient! – David G. Stork Nov 21 '18 at 19:06