Efficient code for minimum integer with given number of factors












7












$begingroup$


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] = 1 because $1$ is the smallest integer that has just a single factor, i.e., ${ 1 }$


  • f[2] = 2 because $2$ is the smallest integer that has just the two factors, i.e., ${ 1, 2 }$


  • f[3] = 4 because $4$ is the smallest integer that has exactly three factors, i.e., ${ 1, 2, 4 }$


  • f[4] = 6 because $6$ is the smallest integer that has exactly four factors, i.e., ${ 1, 2, 3, 6 }$


  • f[5] = 16 because $16$ is the smallest integer that has exactly five factors, i.e., ${ 1, 2, 4, 8, 16 }$


  • f[6] = 12 because $12$ is the smallest integer that has exactly six factors, i.e., ${ 1, 2, 3, 4, 6, 12 }$


  • f[7] = 64 because $64$ is the smallest integer that has exactly seven factors, i.e., ${ 1, 2, 4, 8, 16, 32, 64 }$


  • f[8] = 24 because $24$ is the smallest integer that has exactly eight factors, i.e., ${ 1, 2, 3, 4, 6, 8, 12, 24 }$


  • f[9] = 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, #[[2]] == 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$.



enter image description here



In 1644, the great mathematician Mersenne asked for f[60] = 5040.










share|improve this question











$endgroup$












  • $begingroup$
    f[60] = 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.
    $endgroup$
    – bbgodfrey
    Nov 21 '18 at 3:12












  • $begingroup$
    Yes... but I would like to calculate f[10^6] and even higher, without having to calculate billions of "lower" cases.
    $endgroup$
    – David G. Stork
    Nov 21 '18 at 3:15












  • $begingroup$
    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[4].
    $endgroup$
    – bbgodfrey
    Nov 21 '18 at 3:20










  • $begingroup$
    @bbgodfrey. Oooh... thanks. Fixed. I did the table by hand before I wrote my code. (Bad idea!)
    $endgroup$
    – David G. Stork
    Nov 21 '18 at 3:23










  • $begingroup$
    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.
    $endgroup$
    – J42161217
    Nov 21 '18 at 12:44
















7












$begingroup$


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] = 1 because $1$ is the smallest integer that has just a single factor, i.e., ${ 1 }$


  • f[2] = 2 because $2$ is the smallest integer that has just the two factors, i.e., ${ 1, 2 }$


  • f[3] = 4 because $4$ is the smallest integer that has exactly three factors, i.e., ${ 1, 2, 4 }$


  • f[4] = 6 because $6$ is the smallest integer that has exactly four factors, i.e., ${ 1, 2, 3, 6 }$


  • f[5] = 16 because $16$ is the smallest integer that has exactly five factors, i.e., ${ 1, 2, 4, 8, 16 }$


  • f[6] = 12 because $12$ is the smallest integer that has exactly six factors, i.e., ${ 1, 2, 3, 4, 6, 12 }$


  • f[7] = 64 because $64$ is the smallest integer that has exactly seven factors, i.e., ${ 1, 2, 4, 8, 16, 32, 64 }$


  • f[8] = 24 because $24$ is the smallest integer that has exactly eight factors, i.e., ${ 1, 2, 3, 4, 6, 8, 12, 24 }$


  • f[9] = 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, #[[2]] == 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$.



enter image description here



In 1644, the great mathematician Mersenne asked for f[60] = 5040.










share|improve this question











$endgroup$












  • $begingroup$
    f[60] = 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.
    $endgroup$
    – bbgodfrey
    Nov 21 '18 at 3:12












  • $begingroup$
    Yes... but I would like to calculate f[10^6] and even higher, without having to calculate billions of "lower" cases.
    $endgroup$
    – David G. Stork
    Nov 21 '18 at 3:15












  • $begingroup$
    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[4].
    $endgroup$
    – bbgodfrey
    Nov 21 '18 at 3:20










  • $begingroup$
    @bbgodfrey. Oooh... thanks. Fixed. I did the table by hand before I wrote my code. (Bad idea!)
    $endgroup$
    – David G. Stork
    Nov 21 '18 at 3:23










  • $begingroup$
    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.
    $endgroup$
    – J42161217
    Nov 21 '18 at 12:44














7












7








7





$begingroup$


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] = 1 because $1$ is the smallest integer that has just a single factor, i.e., ${ 1 }$


  • f[2] = 2 because $2$ is the smallest integer that has just the two factors, i.e., ${ 1, 2 }$


  • f[3] = 4 because $4$ is the smallest integer that has exactly three factors, i.e., ${ 1, 2, 4 }$


  • f[4] = 6 because $6$ is the smallest integer that has exactly four factors, i.e., ${ 1, 2, 3, 6 }$


  • f[5] = 16 because $16$ is the smallest integer that has exactly five factors, i.e., ${ 1, 2, 4, 8, 16 }$


  • f[6] = 12 because $12$ is the smallest integer that has exactly six factors, i.e., ${ 1, 2, 3, 4, 6, 12 }$


  • f[7] = 64 because $64$ is the smallest integer that has exactly seven factors, i.e., ${ 1, 2, 4, 8, 16, 32, 64 }$


  • f[8] = 24 because $24$ is the smallest integer that has exactly eight factors, i.e., ${ 1, 2, 3, 4, 6, 8, 12, 24 }$


  • f[9] = 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, #[[2]] == 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$.



enter image description here



In 1644, the great mathematician Mersenne asked for f[60] = 5040.










share|improve this question











$endgroup$




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] = 1 because $1$ is the smallest integer that has just a single factor, i.e., ${ 1 }$


  • f[2] = 2 because $2$ is the smallest integer that has just the two factors, i.e., ${ 1, 2 }$


  • f[3] = 4 because $4$ is the smallest integer that has exactly three factors, i.e., ${ 1, 2, 4 }$


  • f[4] = 6 because $6$ is the smallest integer that has exactly four factors, i.e., ${ 1, 2, 3, 6 }$


  • f[5] = 16 because $16$ is the smallest integer that has exactly five factors, i.e., ${ 1, 2, 4, 8, 16 }$


  • f[6] = 12 because $12$ is the smallest integer that has exactly six factors, i.e., ${ 1, 2, 3, 4, 6, 12 }$


  • f[7] = 64 because $64$ is the smallest integer that has exactly seven factors, i.e., ${ 1, 2, 4, 8, 16, 32, 64 }$


  • f[8] = 24 because $24$ is the smallest integer that has exactly eight factors, i.e., ${ 1, 2, 3, 4, 6, 8, 12, 24 }$


  • f[9] = 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, #[[2]] == 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$.



enter image description here



In 1644, the great mathematician Mersenne asked for f[60] = 5040.







number-theory factorization






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 21 '18 at 6:49







David G. Stork

















asked Nov 21 '18 at 2:12









David G. StorkDavid G. Stork

24.2k22153




24.2k22153












  • $begingroup$
    f[60] = 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.
    $endgroup$
    – bbgodfrey
    Nov 21 '18 at 3:12












  • $begingroup$
    Yes... but I would like to calculate f[10^6] and even higher, without having to calculate billions of "lower" cases.
    $endgroup$
    – David G. Stork
    Nov 21 '18 at 3:15












  • $begingroup$
    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[4].
    $endgroup$
    – bbgodfrey
    Nov 21 '18 at 3:20










  • $begingroup$
    @bbgodfrey. Oooh... thanks. Fixed. I did the table by hand before I wrote my code. (Bad idea!)
    $endgroup$
    – David G. Stork
    Nov 21 '18 at 3:23










  • $begingroup$
    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.
    $endgroup$
    – J42161217
    Nov 21 '18 at 12:44


















  • $begingroup$
    f[60] = 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.
    $endgroup$
    – bbgodfrey
    Nov 21 '18 at 3:12












  • $begingroup$
    Yes... but I would like to calculate f[10^6] and even higher, without having to calculate billions of "lower" cases.
    $endgroup$
    – David G. Stork
    Nov 21 '18 at 3:15












  • $begingroup$
    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[4].
    $endgroup$
    – bbgodfrey
    Nov 21 '18 at 3:20










  • $begingroup$
    @bbgodfrey. Oooh... thanks. Fixed. I did the table by hand before I wrote my code. (Bad idea!)
    $endgroup$
    – David G. Stork
    Nov 21 '18 at 3:23










  • $begingroup$
    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.
    $endgroup$
    – J42161217
    Nov 21 '18 at 12:44
















$begingroup$
f[60] = 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.
$endgroup$
– bbgodfrey
Nov 21 '18 at 3:12






$begingroup$
f[60] = 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.
$endgroup$
– bbgodfrey
Nov 21 '18 at 3:12














$begingroup$
Yes... but I would like to calculate f[10^6] and even higher, without having to calculate billions of "lower" cases.
$endgroup$
– David G. Stork
Nov 21 '18 at 3:15






$begingroup$
Yes... but I would like to calculate f[10^6] and even higher, without having to calculate billions of "lower" cases.
$endgroup$
– David G. Stork
Nov 21 '18 at 3:15














$begingroup$
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[4].
$endgroup$
– bbgodfrey
Nov 21 '18 at 3:20




$begingroup$
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[4].
$endgroup$
– bbgodfrey
Nov 21 '18 at 3:20












$begingroup$
@bbgodfrey. Oooh... thanks. Fixed. I did the table by hand before I wrote my code. (Bad idea!)
$endgroup$
– David G. Stork
Nov 21 '18 at 3:23




$begingroup$
@bbgodfrey. Oooh... thanks. Fixed. I did the table by hand before I wrote my code. (Bad idea!)
$endgroup$
– David G. Stork
Nov 21 '18 at 3:23












$begingroup$
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.
$endgroup$
– J42161217
Nov 21 '18 at 12:44




$begingroup$
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.
$endgroup$
– J42161217
Nov 21 '18 at 12:44










2 Answers
2






active

oldest

votes


















7












$begingroup$

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+1right)$$



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







share|improve this answer









$endgroup$













  • $begingroup$
    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.
    $endgroup$
    – David G. Stork
    Nov 21 '18 at 5:03












  • $begingroup$
    @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.
    $endgroup$
    – bbgodfrey
    Nov 21 '18 at 6:23










  • $begingroup$
    @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.
    $endgroup$
    – bbgodfrey
    Nov 21 '18 at 6:40












  • $begingroup$
    @CarlWoll: Your code is fast enough (accept). (I guess I was too pessimistic in my predictions of how slow "basic" approaches would be.)
    $endgroup$
    – David G. Stork
    Nov 21 '18 at 6:50






  • 1




    $begingroup$
    @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.
    $endgroup$
    – J42161217
    Nov 21 '18 at 11:06



















5












$begingroup$

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.






share|improve this answer









$endgroup$













  • $begingroup$
    Excellent! +1...
    $endgroup$
    – J42161217
    Nov 21 '18 at 19:05










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











Your Answer





StackExchange.ifUsing("editor", function () {
return StackExchange.using("mathjaxEditing", function () {
StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix) {
StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["$", "$"], ["\\(","\\)"]]);
});
});
}, "mathjax-editing");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "387"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmathematica.stackexchange.com%2fquestions%2f186399%2fefficient-code-for-minimum-integer-with-given-number-of-factors%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























2 Answers
2






active

oldest

votes








2 Answers
2






active

oldest

votes









active

oldest

votes






active

oldest

votes









7












$begingroup$

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+1right)$$



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







share|improve this answer









$endgroup$













  • $begingroup$
    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.
    $endgroup$
    – David G. Stork
    Nov 21 '18 at 5:03












  • $begingroup$
    @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.
    $endgroup$
    – bbgodfrey
    Nov 21 '18 at 6:23










  • $begingroup$
    @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.
    $endgroup$
    – bbgodfrey
    Nov 21 '18 at 6:40












  • $begingroup$
    @CarlWoll: Your code is fast enough (accept). (I guess I was too pessimistic in my predictions of how slow "basic" approaches would be.)
    $endgroup$
    – David G. Stork
    Nov 21 '18 at 6:50






  • 1




    $begingroup$
    @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.
    $endgroup$
    – J42161217
    Nov 21 '18 at 11:06
















7












$begingroup$

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+1right)$$



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







share|improve this answer









$endgroup$













  • $begingroup$
    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.
    $endgroup$
    – David G. Stork
    Nov 21 '18 at 5:03












  • $begingroup$
    @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.
    $endgroup$
    – bbgodfrey
    Nov 21 '18 at 6:23










  • $begingroup$
    @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.
    $endgroup$
    – bbgodfrey
    Nov 21 '18 at 6:40












  • $begingroup$
    @CarlWoll: Your code is fast enough (accept). (I guess I was too pessimistic in my predictions of how slow "basic" approaches would be.)
    $endgroup$
    – David G. Stork
    Nov 21 '18 at 6:50






  • 1




    $begingroup$
    @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.
    $endgroup$
    – J42161217
    Nov 21 '18 at 11:06














7












7








7





$begingroup$

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+1right)$$



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







share|improve this answer









$endgroup$



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+1right)$$



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








share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 21 '18 at 4:18









Carl WollCarl Woll

67.5k388175




67.5k388175












  • $begingroup$
    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.
    $endgroup$
    – David G. Stork
    Nov 21 '18 at 5:03












  • $begingroup$
    @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.
    $endgroup$
    – bbgodfrey
    Nov 21 '18 at 6:23










  • $begingroup$
    @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.
    $endgroup$
    – bbgodfrey
    Nov 21 '18 at 6:40












  • $begingroup$
    @CarlWoll: Your code is fast enough (accept). (I guess I was too pessimistic in my predictions of how slow "basic" approaches would be.)
    $endgroup$
    – David G. Stork
    Nov 21 '18 at 6:50






  • 1




    $begingroup$
    @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.
    $endgroup$
    – J42161217
    Nov 21 '18 at 11:06


















  • $begingroup$
    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.
    $endgroup$
    – David G. Stork
    Nov 21 '18 at 5:03












  • $begingroup$
    @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.
    $endgroup$
    – bbgodfrey
    Nov 21 '18 at 6:23










  • $begingroup$
    @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.
    $endgroup$
    – bbgodfrey
    Nov 21 '18 at 6:40












  • $begingroup$
    @CarlWoll: Your code is fast enough (accept). (I guess I was too pessimistic in my predictions of how slow "basic" approaches would be.)
    $endgroup$
    – David G. Stork
    Nov 21 '18 at 6:50






  • 1




    $begingroup$
    @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.
    $endgroup$
    – J42161217
    Nov 21 '18 at 11:06
















$begingroup$
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.
$endgroup$
– David G. Stork
Nov 21 '18 at 5:03






$begingroup$
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.
$endgroup$
– David G. Stork
Nov 21 '18 at 5:03














$begingroup$
@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.
$endgroup$
– bbgodfrey
Nov 21 '18 at 6:23




$begingroup$
@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.
$endgroup$
– bbgodfrey
Nov 21 '18 at 6:23












$begingroup$
@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.
$endgroup$
– bbgodfrey
Nov 21 '18 at 6:40






$begingroup$
@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.
$endgroup$
– bbgodfrey
Nov 21 '18 at 6:40














$begingroup$
@CarlWoll: Your code is fast enough (accept). (I guess I was too pessimistic in my predictions of how slow "basic" approaches would be.)
$endgroup$
– David G. Stork
Nov 21 '18 at 6:50




$begingroup$
@CarlWoll: Your code is fast enough (accept). (I guess I was too pessimistic in my predictions of how slow "basic" approaches would be.)
$endgroup$
– David G. Stork
Nov 21 '18 at 6:50




1




1




$begingroup$
@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.
$endgroup$
– J42161217
Nov 21 '18 at 11:06




$begingroup$
@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.
$endgroup$
– J42161217
Nov 21 '18 at 11:06











5












$begingroup$

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.






share|improve this answer









$endgroup$













  • $begingroup$
    Excellent! +1...
    $endgroup$
    – J42161217
    Nov 21 '18 at 19:05










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
















5












$begingroup$

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.






share|improve this answer









$endgroup$













  • $begingroup$
    Excellent! +1...
    $endgroup$
    – J42161217
    Nov 21 '18 at 19:05










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














5












5








5





$begingroup$

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.






share|improve this answer









$endgroup$



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.







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 21 '18 at 18:53









KennyColnagoKennyColnago

12.1k1754




12.1k1754












  • $begingroup$
    Excellent! +1...
    $endgroup$
    – J42161217
    Nov 21 '18 at 19:05










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


















  • $begingroup$
    Excellent! +1...
    $endgroup$
    – J42161217
    Nov 21 '18 at 19:05










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
















$begingroup$
Excellent! +1...
$endgroup$
– J42161217
Nov 21 '18 at 19:05




$begingroup$
Excellent! +1...
$endgroup$
– J42161217
Nov 21 '18 at 19:05












$begingroup$
@KennyColnago: Very nice (+1)... and reasonable efficient!
$endgroup$
– David G. Stork
Nov 21 '18 at 19:06




$begingroup$
@KennyColnago: Very nice (+1)... and reasonable efficient!
$endgroup$
– David G. Stork
Nov 21 '18 at 19:06


















draft saved

draft discarded




















































Thanks for contributing an answer to Mathematica Stack Exchange!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


Use MathJax to format equations. MathJax reference.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmathematica.stackexchange.com%2fquestions%2f186399%2fefficient-code-for-minimum-integer-with-given-number-of-factors%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

"Incorrect syntax near the keyword 'ON'. (on update cascade, on delete cascade,)

Alcedinidae

RAC Tourist Trophy