Sequence: smallest number with exactly n divisors: Difference between revisions
Sequence: smallest number with exactly n divisors (view source)
Revision as of 11:40, 4 February 2024
, 3 months ago→{{header|Wren}}: Minor tidy
Drkameleon (talk | contribs) (Added Arturo implementation) |
m (→{{header|Wren}}: Minor tidy) |
||
(10 intermediate revisions by 8 users not shown) | |||
Line 20:
{{trans|Python}}
<
V divs = [1]
L(ii) 2 .< Int(n ^ 0.5) + 3
Line 45:
L(item) sequence(15)
print(item)</
{{out}}
Line 68:
=={{header|Action!}}==
Calculations on a real Atari 8-bit computer take quite long time. It is recommended to use an emulator capable with increasing speed of Atari CPU.
<
CARD i,count
Line 114:
PrintC(seq(i))
OD
RETURN</
{{out}}
[https://gitlab.com/amarok8bit/action-rosetta-code/-/raw/master/images/Sequence_smallest_number_with_exactly_n_divisors.png Screenshot from Atari 8-bit computer]
Line 123:
=={{header|ALGOL 68}}==
{{Trans|C}}
<
PROC count divisors = ( INT n )INT:
Line 157:
print( ( newline ) )
END</
{{out}}
<pre>
Line 165:
=={{header|APL}}==
<
{{out}}
<pre>1 2 4 6 16 12 64 24 36 48 1024 60 4096 192 144</pre>
Line 171:
=={{header|Arturo}}==
<
i: 0
while ø [
Line 179:
]
print map 1..15 => firstNumWithDivisors</
{{out}}
Line 187:
=={{header|AutoHotkey}}==
{{trans|Go}}
<
seq := [], n := 0
while (n < max)
Line 202:
count += A_Index = n/A_Index ? 1 : 2
return count
}</
Outputs:<pre>The first 15 terms of the sequence are:
1, 2, 4, 6, 16, 12, 64, 24, 36, 48, 1024, 60, 4096, 192, 144</pre>
=={{header|AWK}}==
<syntaxhighlight lang="awk">
# syntax: GAWK -f SEQUENCE_SMALLEST_NUMBER_WITH_EXACTLY_N_DIVISORS.AWK
# converted from Kotlin
Line 237:
return(count)
}
</syntaxhighlight>
{{out}}
<pre>
first 15 terms: 1 2 4 6 16 12 64 24 36 48 1024 60 4096 192 144
</pre>
=={{header|BASIC}}==
==={{header|BASIC256}}===
{{trans|Ring}}
<syntaxhighlight lang="vbnet">print "the first 15 terms of the sequence are:"
for n = 1 to 15
for m = 1 to 4100
pnum = 0
for p = 1 to 4100
if m % p = 0 then pnum += 1
next p
if pnum = n then
print m; " ";
exit for
end if
next m
next n</syntaxhighlight>
==={{header|Chipmunk Basic}}===
{{trans|Ring}}
{{works with|Chipmunk Basic|3.6.4}}
{{works with|QBasic}}
{{works with|MSX BASIC}}
{{works with|Run BASIC}}
<syntaxhighlight lang="vbnet">100 print "the first 15 terms of the sequence are:"
110 for n = 1 to 15
120 for m = 1 to 4100
130 pnum = 0
140 for p = 1 to 4100
150 if m mod p = 0 then pnum = pnum+1
160 next p
170 if pnum = n then print m;" "; : goto 190
180 next m
190 next n
200 print "done..."
210 end</syntaxhighlight>
==={{header|GW-BASIC}}===
{{works with|PC-BASIC|any}}
{{works with|BASICA}}
The [[#Chipmunk Basic|Chipmunk Basic]] solution works without any changes.
==={{header|MSX Basic}}===
{{works with|MSX BASIC|any}}
The [[#Chipmunk Basic|Chipmunk Basic]] solution works without any changes.
==={{header|QBasic}}===
{{trans|Ring}}
{{works with|QBasic|1.1}}
{{works with|QuickBasic|4.5}}
<syntaxhighlight lang="qbasic">PRINT "the first 15 terms of the sequence are:"
FOR n = 1 to 15
FOR m = 1 to 4100
pnum = 0
FOR p = 1 to 4100
IF m MOD p = 0 then pnum = pnum + 1
NEXT p
IF pnum = n then
PRINT m;
EXIT FOR
END IF
NEXT m
NEXT n</syntaxhighlight>
==={{header|Run BASIC}}===
The [[#Chipmunk Basic|Chipmunk Basic]] solution works without any changes.
==={{header|True BASIC}}===
{{trans|Ring}}
<syntaxhighlight lang="qbasic">PRINT "the first 15 terms of the sequence are:"
FOR n = 1 to 15
FOR m = 1 to 4100
LET pnum = 0
FOR p = 1 to 4100
IF remainder(m, p) = 0 then LET pnum = pnum+1
NEXT p
IF pnum = n then
PRINT m;
EXIT FOR
END IF
NEXT m
NEXT n
PRINT "done..."
END</syntaxhighlight>
==={{header|XBasic}}===
{{trans|Ring}}
{{works with|Windows XBasic}}
<syntaxhighlight lang="qbasic">PROGRAM "program name"
VERSION "0.0000"
DECLARE FUNCTION Entry ()
FUNCTION Entry ()
PRINT "the first 15 terms of the sequence are:"
FOR n = 1 TO 15
FOR m = 1 TO 4100
pnum = 0
FOR p = 1 TO 4100
IF m MOD p = 0 THEN INC pnum
NEXT p
IF pnum = n THEN
PRINT m;
EXIT FOR
END IF
NEXT m
NEXT n
END FUNCTION
END PROGRAM</syntaxhighlight>
==={{header|PureBasic}}===
{{trans|FreeBASIC}}
<syntaxhighlight lang="purebasic">Procedure.i divisors(n)
;find the number of divisors of an integer
Define.i r, i
r = 2
For i = 2 To n/2
If n % i = 0: r + 1
EndIf
Next i
ProcedureReturn r
EndProcedure
OpenConsole()
Define.i UPTO, i, n, nfound
UPTO = 15
i = 2
nfound = 1
Dim sfound.i(UPTO)
sfound(1) = 1
While nfound < UPTO
n = divisors(i)
If n <= UPTO And sfound(n) = 0:
nfound + 1
sfound(n) = i
EndIf
i + 1
Wend
Print("1 ") ;special case
For i = 2 To UPTO
Print(Str(sfound(i)) + " ")
Next i
PrintN(#CRLF$ + "Press ENTER to exit"): Input()
CloseConsole()</syntaxhighlight>
==={{header|Yabasic}}===
{{trans|FreeBASIC}}
<syntaxhighlight lang="vbnet">UPTO = 15
i = 2
nfound = 1
dim sfound(UPTO)
sfound(1) = 1
while nfound < UPTO
n = divisors(i)
if n <= UPTO and sfound(n) = 0 then
nfound = nfound + 1
sfound(n) = i
fi
i = i + 1
end while
print 1, " "; //special case
for i = 2 to UPTO
print sfound(i), " ";
next i
print
end
sub divisors(n)
local r, i
//find the number of divisors of an integer
r = 2
for i = 2 to n / 2
if mod(n, i) = 0 r = r + 1
next i
return r
end sub</syntaxhighlight>
=={{header|BCPL}}==
<
manifest $( LENGTH = 15 $)
Line 277 ⟶ 462:
for i=1 to LENGTH do writef("%N ", seq!i)
wrch('*N')
$)</
{{out}}
<pre>1 2 4 6 16 12 64 24 36 48 1024 60 4096 192 144</pre>
Line 283 ⟶ 468:
=={{header|C}}==
{{trans|Go}}
<
#define MAX 15
Line 314 ⟶ 499:
printf("\n");
return 0;
}</
{{out}}
Line 324 ⟶ 509:
=={{header|C++}}==
{{trans|C}}
<
#define MAX 15
Line 357 ⟶ 542:
cout << endl;
return 0;
}</
{{out}}
Line 366 ⟶ 551:
=={{header|Cowgol}}==
<
const AMOUNT := 15;
Line 408 ⟶ 593:
j := j + 1;
end loop;
print_nl();</
{{out}}
<pre>1 2 4 6 16 12 64 24 36 48 1024 60 4096 192 144</pre>
=={{header|Delphi}}==
{{works with|Delphi|6.0}}
{{libheader|SysUtils,StdCtrls}}
<syntaxhighlight lang="Delphi">
{This code would normally be in a separate library. It is provided for clarity}
function GetAllProperDivisors(N: Integer;var IA: TIntegerDynArray): integer;
{Make a list of all the "proper dividers" for N}
{Proper dividers are the of numbers the divide evenly into N}
var I: integer;
begin
SetLength(IA,0);
for I:=1 to N-1 do
if (N mod I)=0 then
begin
SetLength(IA,Length(IA)+1);
IA[High(IA)]:=I;
end;
Result:=Length(IA);
end;
function GetAllDivisors(N: Integer;var IA: TIntegerDynArray): integer;
{Make a list of all the "proper dividers" for N, Plus N itself}
begin
Result:=GetAllProperDivisors(N,IA)+1;
SetLength(IA,Length(IA)+1);
IA[High(IA)]:=N;
end;
{-------------------------------------------------------------------------------}
procedure SequenceWithNdivisors(Memo: TMemo);
var N,N2: integer;
var IA: TIntegerDynArray;
begin
for N:=1 to 15 do
begin
N2:=0;
repeat Inc(N2) until N=GetAllDivisors(N2,IA);
Memo.Lines.Add(IntToStr(N)+' - '+IntToStr(N2));
end;
end;
</syntaxhighlight>
{{out}}
<pre>
1 - 1
2 - 2
3 - 4
4 - 6
5 - 16
6 - 12
7 - 64
8 - 24
9 - 36
10 - 48
11 - 1024
12 - 60
13 - 4096
14 - 192
15 - 144
Elapsed Time: 54.950 ms.
</pre>
=={{header|EasyLang}}==
<syntaxhighlight lang="easylang">
func cntdiv n .
i = 1
while i <= sqrt n
if n mod i = 0
cnt += 1
if i <> sqrt n
cnt += 1
.
.
i += 1
.
return cnt
.
len seq[] 15
i = 1
while n < 15
k = cntdiv i
if k <= 15 and seq[k] = 0
seq[k] = i
n += 1
.
i += 1
.
for v in seq[]
print v
.
</syntaxhighlight>
=={{header|F_Sharp|F#}}==
This task uses [http://www.rosettacode.org/wiki/Extensible_prime_generator#The_function Extensible Prime Generator (F#)]
<
// Find Antı-Primes plus. Nigel Galloway: April 9th., 2019
// Increasing the value 14 will increase the number of anti-primes plus found
Line 424 ⟶ 713:
let n=Seq.concat(Seq.scan(fun n g->fE n (fG g)) (seq[(2147483647,1,1I)]) fI)|>List.ofSeq|>List.groupBy(fun(_,n,_)->n)|>List.sortBy(fun(n,_)->n)|>List.takeWhile(fun(n,_)->fL n)
for n,g in n do printfn "%d->%A" n (g|>List.map(fun(_,_,n)->n)|>List.min)
</syntaxhighlight>
{{out}}
<pre>
Line 489 ⟶ 778:
=={{header|Factor}}==
<
prettyprint sequences ;
Line 497 ⟶ 786:
] lmap-lazy ;
15 A005179 ltake list>array .</
{{out}}
<pre>
Line 504 ⟶ 793:
=={{header|FreeBASIC}}==
<
function divisors(byval n as ulongint) as uinteger
Line 532 ⟶ 821:
next i
print
end</
{{out}}<pre> 1 2 4 6 16 12 64 24 36 48 1024 60 4096 192 144</pre>
=={{header|Go}}==
<
import "fmt"
Line 565 ⟶ 854:
}
fmt.Println(seq)
}</
{{out}}
Line 575 ⟶ 864:
=={{header|Haskell}}==
Without any subtlety or optimisation, but still fast at this modest scale:
<
import Data.Maybe (mapMaybe)
import Data.Numbers.Primes (primeFactors)
Line 605 ⟶ 894:
[1]
. group
. primeFactors</
{{Out}}
<pre>[1,2,4,6,16,12,64,24,36,48,1024,60,4096,192,144]</pre>
Line 613 ⟶ 902:
Defining a generator in terms of re-usable generic functions, and taking the first 15 terms:
<
'use strict';
Line 827 ⟶ 1,116:
// MAIN ---
return main();
})();</
{{Out}}
<pre>[1,2,4,6,16,12,64,24,36,48,1024,60,4096,192,144]</pre>
Line 834 ⟶ 1,123:
=={{header|J}}==
Rather than factoring by division, this algorithm uses a sieve to tally the factors. Of the whole numbers below ten thousand these are the smallest with n divisors. The list is fully populated through the first 15.
<syntaxhighlight lang="text">
sieve=: 3 :0
range=. <. + i.@:|@:-
Line 844 ⟶ 1,133:
/:~({./.~ {."1) tally,.i.#tally
)
</syntaxhighlight>
<pre>
|: sieve 10000
Line 853 ⟶ 1,142:
=={{header|Java}}==
{{trans|C}}
<
public class OEIS_A005179 {
Line 883 ⟶ 1,172:
System.out.println(Arrays.toString(seq));
}
}</
{{out}}
Line 893 ⟶ 1,182:
=={{header|jq}}==
This entry uses a streaming approach to avoid constructing unnecessary arrays.
<
def divisors:
if . == 1 then 1
Line 920 ⟶ 1,209:
"The first 15 terms of the sequence are:",
[range(1; 16) | A005179]
</syntaxhighlight>
''Invocation'': jq -ncr -f A005179.jq
Line 929 ⟶ 1,218:
=={{header|Julia}}==
{{works with|Julia|1.2}}
<
numfactors(n) = reduce(*, e+1 for (_,e) in factor(n); init=1)
Line 937 ⟶ 1,226:
println("The first 15 terms of the sequence are:")
println(map(A005179, 1:15))
</
<pre>
First 15 terms of OEIS sequence A005179:
Line 945 ⟶ 1,234:
=={{header|Kotlin}}==
{{trans|Go}}
<
const val MAX = 15
Line 975 ⟶ 1,264:
}
println(seq.asList())
}</
{{output}}
Line 984 ⟶ 1,273:
=={{header|Maple}}==
<syntaxhighlight lang="maple">
with(NumberTheory):
Line 999 ⟶ 1,288:
seq(sequenceValue(number), number = 1..15);
</syntaxhighlight>
{{out}}<pre>
1, 2, 4, 6, 16, 12, 64, 24, 36, 48, 1024, 60, 4096, 192, 144
Line 1,005 ⟶ 1,294:
=={{header|Mathematica}} / {{header|Wolfram Language}}==
<
{{out}}
<pre>1 1
Line 1,022 ⟶ 1,311:
14 192
15 144</pre>
=={{header|MiniScript}}==
<syntaxhighlight lang="miniscript">
divisors = function(n)
divs = {1: 1}
divs[n] = 1
i = 2
while i * i <= n
if n % i == 0 then
divs[i] = 1
divs[n / i] = 1
end if
i += 1
end while
return divs.indexes
end function
counts = []
for i in range(1, 15)
j = 1
while divisors(j).len != i
j += 1
end while
counts.push(j)
end for
print "The first 15 terms in the sequence are:"
print counts.join(", ")
</syntaxhighlight>
{{out}}
<pre>
The first 15 terms in the sequence are:
1, 2, 4, 6, 16, 12, 64, 24, 36, 48, 1024, 60, 4096, 192, 144</pre>
=={{header|Miranda}}==
<syntaxhighlight lang="miranda">main :: [sys_message]
main = [Stdout (show (take 15 a005179) ++ "\n")]
a005179 :: [num]
a005179 = map smallest_n_divisors [1..]
smallest_n_divisors :: num->num
smallest_n_divisors n = hd [i | i<-[1..]; n = #divisors i]
divisors :: num->[num]
divisors n = [d | d<-[1..n]; n mod d=0]</syntaxhighlight>
{{out}}
<pre>[1,2,4,6,16,12,64,24,36,48,1024,60,4096,192,144]</pre>
=={{header|Modula-2}}==
<
FROM InOut IMPORT WriteCard, WriteLn;
Line 1,066 ⟶ 1,403:
WriteLn;
END;
END A005179.</
{{out}}
<pre> 1
Line 1,086 ⟶ 1,423:
=={{header|Nanoquery}}==
{{trans|Java}}
<
count = 0
for (i = 1) ((i * i) <= n) (i += 1)
Line 1,113 ⟶ 1,450:
end
end
println seq</
{{out}}
<pre>The first 15 terms of the sequence are:
Line 1,120 ⟶ 1,457:
=={{header|Nim}}==
{{trans|Kotlin}}
<
const MAX = 15
Line 1,146 ⟶ 1,483:
inc n
inc i
echo sequence</
{{out}}
<pre>
Line 1,155 ⟶ 1,492:
=={{header|Perl}}==
{{libheader|ntheory}}
<
use warnings;
use ntheory 'divisors';
Line 1,165 ⟶ 1,502:
print "$l " and last if $n == divisors($l);
}
}</
{{out}}
<pre>First 15 terms of OEIS: A005179
Line 1,172 ⟶ 1,509:
=={{header|Phix}}==
===naive===
<!--<
<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;">15</span>
Line 1,186 ⟶ 1,523:
<span style="color: #008080;">end</span> <span style="color: #008080;">while</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;">"The first %d terms are: %V\n"</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">limit</span><span style="color: #0000FF;">,</span><span style="color: #000000;">res</span><span style="color: #0000FF;">})</span>
<!--</
{{out}}
<pre>
Line 1,195 ⟶ 1,532:
Using the various formula from the OEIS:A005179 link above.<br>
get_primes() and product() have recently been added as new builtins, if necessary see [[Extensible_prime_generator#Phix|Extensible_prime_generator]] and [[Deconvolution/2D%2B#Phix]].
<!--<
<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: #008080;">iff</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">machine_bits</span><span style="color: #0000FF;">()=</span><span style="color: #000000;">32</span><span style="color: #0000FF;">?</span><span style="color: #000000;">58</span><span style="color: #0000FF;">:</span><span style="color: #000000;">66</span><span style="color: #0000FF;">)</span>
Line 1,224 ⟶ 1,561:
<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;">"%d->%d\n"</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">i</span><span style="color: #0000FF;">,</span><span style="color: #000000;">ri</span><span style="color: #0000FF;">})</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<!--</
Note: the f[2]>2 test should really be something more like >log(get_primes(-(lf-1))[$])/log(2),
apparently, but everything seems ok within the IEEE 754 53/64 bit limits this imposes.
Line 1,305 ⟶ 1,642:
and adj are not exactly, um, scientific. Completes in about 0.1s
{{libheader|Phix/mpfr}}
<!--<
<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>
Line 1,427 ⟶ 1,764:
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<span style="color: #0000FF;">{</span><span style="color: #000000;">r</span><span style="color: #0000FF;">,</span><span style="color: #000000;">pn</span><span style="color: #0000FF;">}</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">mpz_free</span><span style="color: #0000FF;">({</span><span style="color: #000000;">r</span><span style="color: #0000FF;">,</span><span style="color: #000000;">pn</span><span style="color: #0000FF;">})</span>
<!--</
{{out}}
<pre>
Line 1,475 ⟶ 1,812:
2^k*3^4*p (made up rule):4
1440 (complete fudge):1
</pre>
=={{header|PROMAL}}==
Builds a table of divisor counts wihout division.
<syntaxhighlight lang="promal">
;;; Find the smallest number with n divisors, n in 1..15
PROGRAM SmallestN
INCLUDE LIBRARY
CON WORD maxDivisors = 15 ; number of sequence elements to find
CON WORD maxNUmber = 6000 ; maximum number we will consider
WORD dc [ maxNumber + 1 ]
WORD firstWithDivisors [ maxDivisors + 1 ]
WORD found
WORD divisors
WORD d
WORD n
WORD j
BEGIN
; compute a table of divisor counts
FOR n = 1 TO maxNumber
dc[ n ] = 0
FOR n = 1 TO maxNumber
j = n
WHILE j <= maxNumber
dc[ j ] = dc[ j ] + 1
j = j + n
; find the first number wih the required divisor counts
FOR n = 0 TO maxDivisors
firstWithDivisors[ n ] = 0
found = 0
n = 0
WHILE found < maxDivisors
n = n + 1
divisors = dc[ n ]
IF divisors <= maxDivisors
IF firstWithDivisors[ divisors ] = 0
; first number with this number of divisors
found = found + 1
firstWithDivisors[ divisors ] = n
FOR d = 1 TO maxDivisors
OUTPUT " #W", firstWithDivisors[ d ]
END
</syntaxhighlight>
{{out}}
<pre>
1 2 4 6 16 12 64 24 36 48 1024 60 4096 192 144
</pre>
=={{header|Python}}==
===Procedural===
<
divs = [1]
for ii in range(2, int(n ** 0.5) + 3):
Line 1,506 ⟶ 1,893:
if __name__ == '__main__':
for item in sequence(15):
print(item)</
{{Out}}
<pre>1
Line 1,528 ⟶ 1,915:
Not optimized, but still fast at this modest scale.
<
from itertools import accumulate, chain, count, groupby, islice, product
Line 1,631 ⟶ 2,018:
# MAIN ---
if __name__ == '__main__':
main()</
{{Out}}
<pre>[1, 2, 4, 6, 16, 12, 64, 24, 36, 48, 1024, 60, 4096, 192, 144]</pre>
Line 1,638 ⟶ 2,025:
<code>factors</code> is defined at [http://rosettacode.org/wiki/Factors_of_an_integer#Quackery Factors of an integer].
<syntaxhighlight lang="quackery"> [ 0
[ 1+ 2dup
factors size =
Line 1,644 ⟶ 2,031:
nip ] is nfactors ( n --> n )
15 times [ i^ 1+ nfactors echo sp ]</
{{out}}
Line 1,652 ⟶ 2,039:
=={{header|R}}==
Could probably be speeded up by caching the results of divisorCount, but it's quick enough already. Not to mention, delightfully short.
<
divisorCount <- function(n) length(Filter(function(x) n %% x == 0, seq_len(n %/% 2))) + 1
smallestWithNDivisors <- function(n)
Line 1,660 ⟶ 2,047:
i
}
print(sapply(1:15, smallestWithNDivisors))</
{{out}}
<pre>[1] 1 2 4 6 16 12 64 24 36 48 1024 60 4096 192 144</pre>
Line 1,668 ⟶ 2,055:
{{works with|Rakudo|2019.03}}
<syntaxhighlight lang="raku"
return 2 if x.is-prime;
+flat (1 .. x.sqrt.floor).map: -> \d {
Line 1,680 ⟶ 2,067:
put (1..$limit).map: -> $n { first { $n == .&div-count }, 1..Inf };
</syntaxhighlight>
{{out}}
<pre>First 15 terms of OEIS:A005179
Line 1,687 ⟶ 2,074:
=={{header|REXX}}==
===some optimization===
<
parse arg N . /*obtain optional argument from the CL.*/
if N=='' | N=="," then N= 15 /*Not specified? Then use the default.*/
Line 1,720 ⟶ 2,107:
else if k*k>x then leave /*only divide up to √ x */
end /*k*/ /* [↑] this form of DO loop is faster.*/
return #+1 /*bump "proper divisors" to "divisors".*/</
{{out|output|text= when using the default input:}}
<pre>
Line 1,742 ⟶ 2,129:
===with memorization===
<
parse arg N lim . /*obtain optional argument from the CL.*/
if N=='' | N=="," then N= 15 /*Not specified? Then use the default.*/
Line 1,777 ⟶ 2,164:
else if k*k>x then leave /*only divide up to √ x */
end /*k*/ /* [↑] this form of DO loop is faster.*/
return #+1 /*bump "proper divisors" to "divisors".*/</
{{out|output|text= is identical to the 1<sup>st</sup> REXX version.}} <br><br>
=={{header|Ring}}==
<
load "stdlib.ring"
Line 1,803 ⟶ 2,190:
see nl + "done..." + nl
</syntaxhighlight>
{{out}}
<pre>
Line 1,811 ⟶ 2,198:
done...
</pre>
=={{header|RPL}}==
{{works with|HP|49g}}
≪ {1}
2 ROT '''FOR''' j
1
'''DO''' 1 + '''UNTIL''' DUP DIVIS SIZE j == '''END'''
+
'''NEXT '''
≫ '<span style="color:blue">TASK</span>' STO
15 <span style="color:blue">TASK</span>
{{out}}
<pre>
1: {1 2 4 6 16 12 64 24 36 48 1024 60 4096 192 144}
</pre>
Runs in 13 minutes on an HP-50g.
====Faster implementation====
With the hint given in the [https://oeis.org/A005179 OEIS page] that a(p)=2^(p-1) when p is prime:
≪ {1}
2 ROT '''FOR''' j
'''IF''' j ISPRIME? '''THEN''' 2 j 1 - ^
'''ELSE'''
1 '''DO''' 1 + '''UNTIL''' DUP DIVIS SIZE j == '''END'''
'''END'''
+
'''NEXT '''
≫ '<span style="color:blue">TASK</span>' STO
the same result is obtained in less than a minute.
=={{header|Ruby}}==
<
def num_divisors(n)
Line 1,824 ⟶ 2,240:
p (1..15).map{|n| first_with_num_divs(n) }
</syntaxhighlight>
{{out}}
<pre>
Line 1,831 ⟶ 2,247:
=={{header|Rust}}==
<
use itertools::Itertools;
Line 2,001 ⟶ 2,417:
}
</syntaxhighlight>
{{out}}
<pre>
Line 2,071 ⟶ 2,487:
64 7560
</pre>
=={{header|SETL}}==
<syntaxhighlight lang="setl">program smallest_number_with_exactly_n_divisors;
print([a(n) : n in [1..15]]);
proc a(n);
return [i +:= 1 : until n = #[d : d in [1..i] | i mod d=0]](i);
end proc;
end program;</syntaxhighlight>
{{out}}
<pre>[1 2 4 6 16 12 64 24 36 48 1024 60 4096 192 144]</pre>
=={{header|Sidef}}==
<
1..Inf -> first_by { .sigma0 == n }
}
say 15.of { n_divisors(_+1) }</
{{out}}
Line 2,085 ⟶ 2,512:
More efficient solution:
<
if (solutions == threshold) {
Line 2,107 ⟶ 2,534:
say n_divisors(60) #=> 5040
say n_divisors(1000) #=> 810810000</
=={{header|Swift}}==
<
func divisorCount(number: Int) -> Int {
var n = number
Line 2,152 ⟶ 2,579:
print(n, terminator: " ")
}
print()</
{{out}}
Line 2,160 ⟶ 2,587:
=={{header|Tcl}}==
<
set cnt 0
for {set d 1} {($d * $d) <= $n} {incr d} {
Line 2,193 ⟶ 2,620:
show A005179 1 15
</syntaxhighlight>
{{out}}
<pre>A005179(1..15) =
Line 2,200 ⟶ 2,627:
=={{header|Wren}}==
{{libheader|Wren-math}}
<
var limit = 22
Line 2,214 ⟶ 2,641:
}
System.print("The first %(limit) terms are:")
System.print(numbers)</
{{out}}
Line 2,223 ⟶ 2,650:
=={{header|XPL0}}==
<
int N, Count, D;
[Count:= 0;
Line 2,237 ⟶ 2,664:
IntOut(0, AN); ChOut(0, ^ );
];
]</
{{out}}
Line 2,245 ⟶ 2,672:
=={{header|zkl}}==
<
{ [1.. n.toFloat().sqrt()].reduce('wrap(s,i){ s + (if(0==n%i) 1 + (i!=n/i)) },0) }
A005179w:=(1).walker(*).tweak(fcn(n){
Line 2,254 ⟶ 2,681:
if(n<d and not cache.find(d)) cache[d]=N;
}
});</
<
println("First %d terms of OEIS:A005179".fmt(N));
A005179w.walk(N).concat(" ").println();</
{{out}}
<pre>
|