Sequence: nth number with exactly n divisors: Difference between revisions

Content added Content deleted
m (J)
m (syntax highlighting fixup automation)
Line 17: Line 17:
=={{header|C}}==
=={{header|C}}==
{{trans|C++}}
{{trans|C++}}
<lang c>#include <math.h>
<syntaxhighlight lang="c">#include <math.h>
#include <stdbool.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdint.h>
Line 135: Line 135:


return 0;
return 0;
}</lang>
}</syntaxhighlight>
{{out}}
{{out}}
<pre>A073916(1) = 1
<pre>A073916(1) = 1
Line 155: Line 155:
=={{header|C++}}==
=={{header|C++}}==
{{trans|Java}}
{{trans|Java}}
<lang cpp>#include <iostream>
<syntaxhighlight lang="cpp">#include <iostream>
#include <vector>
#include <vector>


Line 246: Line 246:


return 0;
return 0;
}</lang>
}</syntaxhighlight>
{{out}}
{{out}}
<pre>A073916(1) = 1
<pre>A073916(1) = 1
Line 266: Line 266:
=={{header|D}}==
=={{header|D}}==
{{trans|Java}}
{{trans|Java}}
<lang d>import std.bigint;
<syntaxhighlight lang="d">import std.bigint;
import std.math;
import std.math;
import std.stdio;
import std.stdio;
Line 355: Line 355:
writeln("A073916(", n, ") = ", OEISA073916(n));
writeln("A073916(", n, ") = ", OEISA073916(n));
}
}
}</lang>
}</syntaxhighlight>
{{out}}
{{out}}
<pre>A073916(1) = 1
<pre>A073916(1) = 1
Line 405: Line 405:
=={{header|Factor}}==
=={{header|Factor}}==
This makes use of most of the optimizations discussed in the Go example.
This makes use of most of the optimizations discussed in the Go example.
<lang factor>USING: combinators formatting fry kernel lists lists.lazy
<syntaxhighlight lang="factor">USING: combinators formatting fry kernel lists lists.lazy
lists.lazy.examples literals math math.functions math.primes
lists.lazy.examples literals math math.functions math.primes
math.primes.factors math.ranges sequences ;
math.primes.factors math.ranges sequences ;
Line 436: Line 436:
: main ( -- ) 45 [1,b] [ dup fn "%2d : %d\n" printf ] each ;
: main ( -- ) 45 [1,b] [ dup fn "%2d : %d\n" printf ] each ;


MAIN: main</lang>
MAIN: main</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 489: Line 489:
=={{header|FreeBASIC}}==
=={{header|FreeBASIC}}==
But it takes an "eternity" to resolve.
But it takes an "eternity" to resolve.
<lang freebasic>Dim As Integer n, num, pnum
<syntaxhighlight lang="freebasic">Dim As Integer n, num, pnum
Dim As Ulongint m, p
Dim As Ulongint m, p
Dim As Ulongint limit = 18446744073709551615
Dim As Ulongint limit = 18446744073709551615
Line 508: Line 508:
Next m
Next m
Next n
Next n
Sleep</lang>
Sleep</syntaxhighlight>




Line 515: Line 515:


The remaining terms (up to the 33rd) are not particularly large and so are calculated by brute force.
The remaining terms (up to the 33rd) are not particularly large and so are calculated by brute force.
<lang go>package main
<syntaxhighlight lang="go">package main


import (
import (
Line 597: Line 597:
}
}
}
}
}</lang>
}</syntaxhighlight>


{{out}}
{{out}}
Line 645: Line 645:
3. While searching for the nth number with exactly n divisors, where feasible a record is kept of any numbers found to have exactly k divisors (k > n) so that the search for these numbers can start from a higher base.
3. While searching for the nth number with exactly n divisors, where feasible a record is kept of any numbers found to have exactly k divisors (k > n) so that the search for these numbers can start from a higher base.


<lang go>package main
<syntaxhighlight lang="go">package main


import (
import (
Line 765: Line 765:
}
}
}
}
}</lang>
}</syntaxhighlight>


{{out}}
{{out}}
Line 817: Line 817:
</pre>
</pre>
=={{header|Haskell}}==
=={{header|Haskell}}==
<lang haskell>import Control.Monad (guard)
<syntaxhighlight lang="haskell">import Control.Monad (guard)
import Math.NumberTheory.ArithmeticFunctions (divisorCount)
import Math.NumberTheory.ArithmeticFunctions (divisorCount)
import Math.NumberTheory.Primes (Prime, unPrime)
import Math.NumberTheory.Primes (Prime, unPrime)
Line 844: Line 844:


main :: IO ()
main :: IO ()
main = mapM_ print nths</lang>
main = mapM_ print nths</syntaxhighlight>
{{out}}
{{out}}
<pre>(1,1)
<pre>(1,1)
Line 886: Line 886:
Implementation:
Implementation:


<syntaxhighlight lang="j">
<lang J>
A073916=: {{
A073916=: {{
if.1 p: y do. (p:^x:)y-1 return.
if.1 p: y do. (p:^x:)y-1 return.
Line 901: Line 901:
(y-1){r
(y-1){r
}}"0
}}"0
</syntaxhighlight>
</lang>


Task example:
Task example:


<syntaxhighlight lang="j">
<lang J>
(,. A073916) 1+i.20
(,. A073916) 1+i.20
1 1
1 1
Line 927: Line 927:
19 740195513856780056217081017732809
19 740195513856780056217081017732809
20 1520
20 1520
</syntaxhighlight>
</lang>


=={{header|Java}}==
=={{header|Java}}==
Line 933: Line 933:
Replace translation with Java native implementation.
Replace translation with Java native implementation.


<lang java>
<syntaxhighlight lang="java">
import java.math.BigInteger;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.ArrayList;
Line 1,022: Line 1,022:


}
}
</syntaxhighlight>
</lang>


{{out}}
{{out}}
Line 1,086: Line 1,086:


'''Preliminaries'''
'''Preliminaries'''
<lang jq>def count(stream): reduce stream as $i (0; .+1);
<syntaxhighlight lang="jq">def count(stream): reduce stream as $i (0; .+1);


# To maintain precision:
# To maintain precision:
Line 1,109: Line 1,109:
end
end
end;
end;
</syntaxhighlight>
</lang>
'''The Task'''
'''The Task'''
<lang jq># Emit [n, nth_with_n_divisors] for n in range(1; .+1)
<syntaxhighlight lang="jq"># Emit [n, nth_with_n_divisors] for n in range(1; .+1)
def nth_with_n_divisors:
def nth_with_n_divisors:
| [limit( .; primes)] as $primes
| [limit( .; primes)] as $primes
Line 1,137: Line 1,137:


"The first 33 terms in the sequence are:",
"The first 33 terms in the sequence are:",
(33 | nth_with_n_divisors)</lang>
(33 | nth_with_n_divisors)</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 1,177: Line 1,177:


=={{header|Julia}}==
=={{header|Julia}}==
<lang julia>using Primes
<syntaxhighlight lang="julia">using Primes


function countdivisors(n)
function countdivisors(n)
Line 1,206: Line 1,206:


nthwithndivisors(35)
nthwithndivisors(35)
</lang>{{out}}
</syntaxhighlight>{{out}}
<pre>
<pre>
1 : 1
1 : 1
Line 1,247: Line 1,247:
=={{header|Kotlin}}==
=={{header|Kotlin}}==
{{trans|Go}}
{{trans|Go}}
<lang scala>// Version 1.3.21
<syntaxhighlight lang="scala">// Version 1.3.21


import java.math.BigInteger
import java.math.BigInteger
Line 1,325: Line 1,325:
}
}
}
}
}</lang>
}</syntaxhighlight>


{{output}}
{{output}}
Line 1,366: Line 1,366:


=={{header|Mathematica}} / {{header|Wolfram Language}}==
=={{header|Mathematica}} / {{header|Wolfram Language}}==
<lang Mathematica>d = Table[
<syntaxhighlight lang="mathematica">d = Table[
Length[Divisors[n]], {n,
Length[Divisors[n]], {n,
200000}]; t = {}; n = 0; ok = True; While[ok, n++;
200000}]; t = {}; n = 0; ok = True; While[ok, n++;
Line 1,372: Line 1,372:
c = Flatten[Position[d, n, 1, n]];
c = Flatten[Position[d, n, 1, n]];
If[Length[c] >= n, AppendTo[t, c[[n]]], ok = False]]];
If[Length[c] >= n, AppendTo[t, c[[n]]], ok = False]]];
t</lang>
t</syntaxhighlight>
{{out}}
{{out}}
<pre>{1,3,25,14,14641,44,24137569,70,1089,405,819628286980801,160,22563490300366186081,2752,9801,462,21559177407076402401757871041,1044,740195513856780056217081017732809,1520,141376,84992,1658509762573818415340429240403156732495289,1170}</pre>
<pre>{1,3,25,14,14641,44,24137569,70,1089,405,819628286980801,160,22563490300366186081,2752,9801,462,21559177407076402401757871041,1044,740195513856780056217081017732809,1520,141376,84992,1658509762573818415340429240403156732495289,1170}</pre>
Line 1,380: Line 1,380:
{{libheader|bignum}}
{{libheader|bignum}}
This is a translation of the fast Go version. It runs in about 23s on our laptop.
This is a translation of the fast Go version. It runs in about 23s on our laptop.
<lang Nim>import math, strformat
<syntaxhighlight lang="nim">import math, strformat
import bignum
import bignum


Line 1,458: Line 1,458:
k > records[cd].num and (d == 1 or d == 2 and not cd.isOdd):
k > records[cd].num and (d == 1 or d == 2 and not cd.isOdd):
records[cd].num = k
records[cd].num = k
inc records[cd].count</lang>
inc records[cd].count</syntaxhighlight>


{{out}}
{{out}}
Line 1,511: Line 1,511:
{{libheader|ntheory}}
{{libheader|ntheory}}
{{trans|Raku}}
{{trans|Raku}}
<lang perl>use strict;
<syntaxhighlight lang="perl">use strict;
use warnings;
use warnings;
use bigint;
use bigint;
Line 1,531: Line 1,531:
}
}
}
}
}</lang>
}</syntaxhighlight>
{{out}}
{{out}}
<pre>First 20 terms of OEIS:A073916
<pre>First 20 terms of OEIS:A073916
Line 1,541: Line 1,541:
Certainly not the fastest way to do it, hence the relatively small limit of 24, which takes less than 0.4s,<br>
Certainly not the fastest way to do it, hence the relatively small limit of 24, which takes less than 0.4s,<br>
whereas a limit of 25 would need to invoke factors() 52 million times which would no doubt take a fair while.
whereas a limit of 25 would need to invoke factors() 52 million times which would no doubt take a fair while.
<!--<lang Phix>(phixonline)-->
<!--<syntaxhighlight lang="phix">(phixonline)-->
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
<span style="color: #008080;">constant</span> <span style="color: #000000;">LIMIT</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">24</span>
<span style="color: #008080;">constant</span> <span style="color: #000000;">LIMIT</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">24</span>
Line 1,565: Line 1,565:
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<!--</lang>-->
<!--</syntaxhighlight>-->
{{out}}
{{out}}
<pre>
<pre>
Line 1,597: Line 1,597:
===cheating slightly===
===cheating slightly===
No real patterns that I could see here, but you can still identify and single out the troublemakers (of which there are about 30).
No real patterns that I could see here, but you can still identify and single out the troublemakers (of which there are about 30).
<!--<lang Phix>(phixonline)-->
<!--<syntaxhighlight lang="phix">(phixonline)-->
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
<span style="color: #008080;">include</span> <span style="color: #004080;">mpfr</span><span style="color: #0000FF;">.</span><span style="color: #000000;">e</span>
<span style="color: #008080;">include</span> <span style="color: #004080;">mpfr</span><span style="color: #0000FF;">.</span><span style="color: #000000;">e</span>
Line 1,662: Line 1,662:
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"completed in %s\n"</span><span style="color: #0000FF;">,{</span><span style="color: #7060A8;">elapsed</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">time</span><span style="color: #0000FF;">()-</span><span style="color: #000000;">t0</span><span style="color: #0000FF;">)})</span>
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"completed in %s\n"</span><span style="color: #0000FF;">,{</span><span style="color: #7060A8;">elapsed</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">time</span><span style="color: #0000FF;">()-</span><span style="color: #000000;">t0</span><span style="color: #0000FF;">)})</span>
<!--</lang>-->
<!--</syntaxhighlight>-->
{{out}}
{{out}}
<pre>
<pre>
Line 1,771: Line 1,771:
=={{header|Python}}==
=={{header|Python}}==
This implementation exploits the fact that terms corresponding to a prime value for n are always the nth prime to the (n-1)th power.
This implementation exploits the fact that terms corresponding to a prime value for n are always the nth prime to the (n-1)th power.
<syntaxhighlight lang="python">
<lang Python>
def divisors(n):
def divisors(n):
divs = [1]
divs = [1]
Line 1,835: Line 1,835:
for item in sequence(15):
for item in sequence(15):
print(item)
print(item)
</syntaxhighlight>
</lang>
<b>Output:</b>
<b>Output:</b>
<syntaxhighlight lang="python">
<lang Python>
1
1
3
3
Line 1,853: Line 1,853:
2752
2752
9801
9801
</syntaxhighlight>
</lang>


=={{header|Raku}}==
=={{header|Raku}}==
Line 1,861: Line 1,861:
[https://tio.run/##dVLbTsJAEH3nKw4IpgW6ATQY2QAao4kvmsijEFPoVpr05nZr2hh@yk/wx@p0CwUf3JfuzJwzc/ZMYyH9cVEk6RqO92ltojRUMJaZia8G6EihUhliBM9FxrzEiqUXCK5rPde3CTwEY1RLPqRirh9F0mSBHU9gzbB09m3Kk4a@SBJk6IDSCHIsc0wppsFwOCiYUmU@p1uzCSPvwzGx0/xdg74NorR9L/AU0UYDXmVutKKEUu9SxNS4VoldH0PGuryxx7xW7BXHGSqE2grYUto5w@Lu6YU6xqlC68GTiTrMUkIGCSIXz/ePi8nt4OriejhucY00qH8FM9k2j4U0JqO1rTbbowftcO@BQbcZLmGHDiVrlSa9WNdFFpcQC8M@asE6XplkiMY40YmhpR0evXvA/6ZIsK0iSRWidzq0PPK0VNo1tbH6Vunr/nwfyTWTueX7JyejyhOKTB2WSI1pWey8/mf4v9BerxRZavmLab/VYb1jXhS/ Try it online!]
[https://tio.run/##dVLbTsJAEH3nKw4IpgW6ATQY2QAao4kvmsijEFPoVpr05nZr2hh@yk/wx@p0CwUf3JfuzJwzc/ZMYyH9cVEk6RqO92ltojRUMJaZia8G6EihUhliBM9FxrzEiqUXCK5rPde3CTwEY1RLPqRirh9F0mSBHU9gzbB09m3Kk4a@SBJk6IDSCHIsc0wppsFwOCiYUmU@p1uzCSPvwzGx0/xdg74NorR9L/AU0UYDXmVutKKEUu9SxNS4VoldH0PGuryxx7xW7BXHGSqE2grYUto5w@Lu6YU6xqlC68GTiTrMUkIGCSIXz/ePi8nt4OriejhucY00qH8FM9k2j4U0JqO1rTbbowftcO@BQbcZLmGHDiVrlSa9WNdFFpcQC8M@asE6XplkiMY40YmhpR0evXvA/6ZIsK0iSRWidzq0PPK0VNo1tbH6Vunr/nwfyTWTueX7JyejyhOKTB2WSI1pWey8/mf4v9BerxRZavmLab/VYb1jXhS/ Try it online!]


<lang perl6>sub div-count (\x) {
<syntaxhighlight lang="raku" line>sub div-count (\x) {
return 2 if x.is-prime;
return 2 if x.is-prime;
+flat (1 .. x.sqrt.floor).map: -> \d {
+flat (1 .. x.sqrt.floor).map: -> \d {
Line 1,886: Line 1,886:
}
}
}
}
};</lang>
};</syntaxhighlight>


<pre>First 20 terms of OEIS:A073916
<pre>First 20 terms of OEIS:A073916
Line 1,894: Line 1,894:
Programming note: &nbsp; this REXX version has minor optimization, and all terms of the sequence are determined (found) in order.
Programming note: &nbsp; this REXX version has minor optimization, and all terms of the sequence are determined (found) in order.
===little optimization===
===little optimization===
<lang rexx>/*REXX program finds and displays the Nth number with exactly N divisors. */
<syntaxhighlight lang="rexx">/*REXX program finds and displays the Nth number with exactly N divisors. */
parse arg N . /*obtain optional argument from the CL.*/
parse arg N . /*obtain optional argument from the CL.*/
if N=='' | N=="," then N= 15 /*Not specified? Then use the default.*/
if N=='' | N=="," then N= 15 /*Not specified? Then use the default.*/
Line 1,953: Line 1,953:
do j=11 by 6 until j*j>#; if # // j==0 | # // (J+2)==0 then return 0
do j=11 by 6 until j*j>#; if # // j==0 | # // (J+2)==0 then return 0
end /*j*/ /* ___ */
end /*j*/ /* ___ */
return 1 /*Exceeded √ # ? Then # is prime. */</lang>
return 1 /*Exceeded √ # ? Then # is prime. */</syntaxhighlight>
{{out|output|text=&nbsp; when using the input: &nbsp; &nbsp; <tt> 45 </tt>}}
{{out|output|text=&nbsp; when using the input: &nbsp; &nbsp; <tt> 45 </tt>}}


Line 2,024: Line 2,024:
This REXX version (unlike the 1<sup>st</sup> version), &nbsp; only goes through the numbers once, instead
This REXX version (unlike the 1<sup>st</sup> version), &nbsp; only goes through the numbers once, instead
of looking for numbers that have specific number of divisors.
of looking for numbers that have specific number of divisors.
<lang rexx>/*REXX program finds and displays the Nth number with exactly N divisors. */
<syntaxhighlight lang="rexx">/*REXX program finds and displays the Nth number with exactly N divisors. */
parse arg N . /*obtain optional argument from the CL.*/
parse arg N . /*obtain optional argument from the CL.*/
if N=='' | N=="," then N= 15 /*Not specified? Then use the default.*/
if N=='' | N=="," then N= 15 /*Not specified? Then use the default.*/
Line 2,100: Line 2,100:
if #//(i+2)==0 then return 0
if #//(i+2)==0 then return 0
end /*i*/ /* ___ */
end /*i*/ /* ___ */
return 1 /*Exceeded √ # ? Then # is prime. */</lang>
return 1 /*Exceeded √ # ? Then # is prime. */</syntaxhighlight>
{{out|output|text=&nbsp; is identical to the 1<sup>st</sup> REXX version.}} <br><br>
{{out|output|text=&nbsp; is identical to the 1<sup>st</sup> REXX version.}} <br><br>


=={{header|Ring}}==
=={{header|Ring}}==
<lang ring>
<syntaxhighlight lang="ring">
load "stdlib.ring"
load "stdlib.ring"


Line 2,133: Line 2,133:


see nl + "done..." + nl
see nl + "done..." + nl
</syntaxhighlight>
</lang>
{{out}}
{{out}}
<pre>
<pre>
Line 2,158: Line 2,158:
=={{header|Ruby}}==
=={{header|Ruby}}==
{{trans|Java}}
{{trans|Java}}
<lang ruby>def isPrime(n)
<syntaxhighlight lang="ruby">def isPrime(n)
return false if n < 2
return false if n < 2
return n == 2 if n % 2 == 0
return n == 2 if n % 2 == 0
Line 2,246: Line 2,246:
print "A073916(", n, ") = ", OEISA073916(n), "\n"
print "A073916(", n, ") = ", OEISA073916(n), "\n"
n = n + 1
n = n + 1
end</lang>
end</syntaxhighlight>
{{out}}
{{out}}
<pre>A073916(1) = 1
<pre>A073916(1) = 1
Line 2,265: Line 2,265:


=={{header|Sidef}}==
=={{header|Sidef}}==
<lang ruby>func f(n {.is_prime}) {
<syntaxhighlight lang="ruby">func f(n {.is_prime}) {
n.prime**(n-1)
n.prime**(n-1)
}
}
Line 2,273: Line 2,273:
}
}


say 20.of { f(_+1) }</lang>
say 20.of { f(_+1) }</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 2,284: Line 2,284:
{{libheader|Wren-big}}
{{libheader|Wren-big}}
{{libheader|Wren-fmt}}
{{libheader|Wren-fmt}}
<lang ecmascript>import "/math" for Int
<syntaxhighlight lang="ecmascript">import "/math" for Int
import "/big" for BigInt
import "/big" for BigInt
import "/fmt" for Fmt
import "/fmt" for Fmt
Line 2,319: Line 2,319:
}
}
}
}
}</lang>
}</syntaxhighlight>


{{out}}
{{out}}
Line 2,365: Line 2,365:


[[Extensible prime generator#zkl]] could be used instead.
[[Extensible prime generator#zkl]] could be used instead.
<lang zkl>var [const] BI=Import("zklBigNum"), pmax=25; // libGMP
<syntaxhighlight lang="zkl">var [const] BI=Import("zklBigNum"), pmax=25; // libGMP
p:=BI(1);
p:=BI(1);
primes:=pmax.pump(List(0), p.nextPrime, "copy"); //-->(0,3,5,7,11,13,17,19,...)
primes:=pmax.pump(List(0), p.nextPrime, "copy"); //-->(0,3,5,7,11,13,17,19,...)
Line 2,404: Line 2,404:
}
}
}
}
}</lang>
}</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>