Efficient code for minimum integer with given number of factors
up vote
7
down vote
favorite
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$.
In 1644, the great mathematician Mersenne asked for f[60] = 5040
.
number-theory factorization
|
show 1 more comment
up vote
7
down vote
favorite
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$.
In 1644, the great mathematician Mersenne asked for f[60] = 5040
.
number-theory factorization
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.
– bbgodfrey
Nov 21 at 3:12
Yes... but I would like to calculatef[10^6]
and even higher, without having to calculate billions of "lower" cases.
– David G. Stork
Nov 21 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 yourf[4]
.
– bbgodfrey
Nov 21 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 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 at 12:44
|
show 1 more comment
up vote
7
down vote
favorite
up vote
7
down vote
favorite
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$.
In 1644, the great mathematician Mersenne asked for f[60] = 5040
.
number-theory factorization
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$.
In 1644, the great mathematician Mersenne asked for f[60] = 5040
.
number-theory factorization
number-theory factorization
edited Nov 21 at 6:49
asked Nov 21 at 2:12
David G. Stork
22.7k22050
22.7k22050
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.
– bbgodfrey
Nov 21 at 3:12
Yes... but I would like to calculatef[10^6]
and even higher, without having to calculate billions of "lower" cases.
– David G. Stork
Nov 21 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 yourf[4]
.
– bbgodfrey
Nov 21 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 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 at 12:44
|
show 1 more comment
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.
– bbgodfrey
Nov 21 at 3:12
Yes... but I would like to calculatef[10^6]
and even higher, without having to calculate billions of "lower" cases.
– David G. Stork
Nov 21 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 yourf[4]
.
– bbgodfrey
Nov 21 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 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 at 12:44
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.– bbgodfrey
Nov 21 at 3:12
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.– bbgodfrey
Nov 21 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 at 3:15
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 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[4]
.– bbgodfrey
Nov 21 at 3:20
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]
.– bbgodfrey
Nov 21 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 at 3:23
@bbgodfrey. Oooh... thanks. Fixed. I did the table by hand before I wrote my code. (Bad idea!)
– David G. Stork
Nov 21 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 at 12:44
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 at 12:44
|
show 1 more comment
2 Answers
2
active
oldest
votes
up vote
7
down vote
accepted
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
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 at 5:03
@DavidG.Stork Consider why the8
case fails.8
can be represented by{{2, 3}}
, which yieldsf = {1,1,1}
andInverseDivisor
of30
. However,8
also can be represented by{{2, 1}, {4, 1}}
, which yieldsf = {3,1}
andInverseDivisor
of24
. Generalizing, it appears that not only must theIntegerFactor
s of 'n` be considered but also products of those factors: Analyze all sets of products of factors and chose the one that yields the smallestInverseDivisor
. Though slower than the approach in the answer, it is vastly faster than the brute force approach in the question.
– bbgodfrey
Nov 21 at 6:23
@DavidG.Stork More generally, it appears that most cases that are multiples of8
require the approach outlined in my comment immediately above. I have not found any other cases requiring that approach.
– bbgodfrey
Nov 21 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 at 6:50
1
@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 at 11:06
|
show 2 more comments
up vote
5
down vote
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.
Excellent! +1...
– J42161217
Nov 21 at 19:05
@KennyColnago: Very nice (+1)... and reasonable efficient!
– David G. Stork
Nov 21 at 19:06
add a comment |
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
7
down vote
accepted
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
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 at 5:03
@DavidG.Stork Consider why the8
case fails.8
can be represented by{{2, 3}}
, which yieldsf = {1,1,1}
andInverseDivisor
of30
. However,8
also can be represented by{{2, 1}, {4, 1}}
, which yieldsf = {3,1}
andInverseDivisor
of24
. Generalizing, it appears that not only must theIntegerFactor
s of 'n` be considered but also products of those factors: Analyze all sets of products of factors and chose the one that yields the smallestInverseDivisor
. Though slower than the approach in the answer, it is vastly faster than the brute force approach in the question.
– bbgodfrey
Nov 21 at 6:23
@DavidG.Stork More generally, it appears that most cases that are multiples of8
require the approach outlined in my comment immediately above. I have not found any other cases requiring that approach.
– bbgodfrey
Nov 21 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 at 6:50
1
@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 at 11:06
|
show 2 more comments
up vote
7
down vote
accepted
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
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 at 5:03
@DavidG.Stork Consider why the8
case fails.8
can be represented by{{2, 3}}
, which yieldsf = {1,1,1}
andInverseDivisor
of30
. However,8
also can be represented by{{2, 1}, {4, 1}}
, which yieldsf = {3,1}
andInverseDivisor
of24
. Generalizing, it appears that not only must theIntegerFactor
s of 'n` be considered but also products of those factors: Analyze all sets of products of factors and chose the one that yields the smallestInverseDivisor
. Though slower than the approach in the answer, it is vastly faster than the brute force approach in the question.
– bbgodfrey
Nov 21 at 6:23
@DavidG.Stork More generally, it appears that most cases that are multiples of8
require the approach outlined in my comment immediately above. I have not found any other cases requiring that approach.
– bbgodfrey
Nov 21 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 at 6:50
1
@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 at 11:06
|
show 2 more comments
up vote
7
down vote
accepted
up vote
7
down vote
accepted
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
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
answered Nov 21 at 4:18
Carl Woll
66.2k385173
66.2k385173
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 at 5:03
@DavidG.Stork Consider why the8
case fails.8
can be represented by{{2, 3}}
, which yieldsf = {1,1,1}
andInverseDivisor
of30
. However,8
also can be represented by{{2, 1}, {4, 1}}
, which yieldsf = {3,1}
andInverseDivisor
of24
. Generalizing, it appears that not only must theIntegerFactor
s of 'n` be considered but also products of those factors: Analyze all sets of products of factors and chose the one that yields the smallestInverseDivisor
. Though slower than the approach in the answer, it is vastly faster than the brute force approach in the question.
– bbgodfrey
Nov 21 at 6:23
@DavidG.Stork More generally, it appears that most cases that are multiples of8
require the approach outlined in my comment immediately above. I have not found any other cases requiring that approach.
– bbgodfrey
Nov 21 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 at 6:50
1
@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 at 11:06
|
show 2 more comments
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 at 5:03
@DavidG.Stork Consider why the8
case fails.8
can be represented by{{2, 3}}
, which yieldsf = {1,1,1}
andInverseDivisor
of30
. However,8
also can be represented by{{2, 1}, {4, 1}}
, which yieldsf = {3,1}
andInverseDivisor
of24
. Generalizing, it appears that not only must theIntegerFactor
s of 'n` be considered but also products of those factors: Analyze all sets of products of factors and chose the one that yields the smallestInverseDivisor
. Though slower than the approach in the answer, it is vastly faster than the brute force approach in the question.
– bbgodfrey
Nov 21 at 6:23
@DavidG.Stork More generally, it appears that most cases that are multiples of8
require the approach outlined in my comment immediately above. I have not found any other cases requiring that approach.
– bbgodfrey
Nov 21 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 at 6:50
1
@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 at 11:06
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 at 5:03
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 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 IntegerFactor
s 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 at 6:23
@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 IntegerFactor
s 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 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 at 6:40
@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 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 at 6:50
@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 at 6:50
1
1
@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 at 11:06
@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 at 11:06
|
show 2 more comments
up vote
5
down vote
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.
Excellent! +1...
– J42161217
Nov 21 at 19:05
@KennyColnago: Very nice (+1)... and reasonable efficient!
– David G. Stork
Nov 21 at 19:06
add a comment |
up vote
5
down vote
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.
Excellent! +1...
– J42161217
Nov 21 at 19:05
@KennyColnago: Very nice (+1)... and reasonable efficient!
– David G. Stork
Nov 21 at 19:06
add a comment |
up vote
5
down vote
up vote
5
down vote
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.
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.
answered Nov 21 at 18:53
KennyColnago
12k1754
12k1754
Excellent! +1...
– J42161217
Nov 21 at 19:05
@KennyColnago: Very nice (+1)... and reasonable efficient!
– David G. Stork
Nov 21 at 19:06
add a comment |
Excellent! +1...
– J42161217
Nov 21 at 19:05
@KennyColnago: Very nice (+1)... and reasonable efficient!
– David G. Stork
Nov 21 at 19:06
Excellent! +1...
– J42161217
Nov 21 at 19:05
Excellent! +1...
– J42161217
Nov 21 at 19:05
@KennyColnago: Very nice (+1)... and reasonable efficient!
– David G. Stork
Nov 21 at 19:06
@KennyColnago: Very nice (+1)... and reasonable efficient!
– David G. Stork
Nov 21 at 19:06
add a comment |
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.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- 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.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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.– bbgodfrey
Nov 21 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 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 yourf[4]
.– bbgodfrey
Nov 21 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 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 at 12:44